Assets go to internal and external storage

Source: Internet
Author: User

Layout

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"Tools:context="Com.hanqi.cunchu.lianxi"><ImageView android:layout_width="70DP"Android:layout_height="70DP"Android:id="@+id/iv"/> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Assets File Pictures"/> <ImageView android:layout_width="70DP"Android:layout_height="70DP"Android:id="@+id/iv2"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="assets go to internal storage and display"Android:onclick="BT"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="internal storage goes to external"Android:onclick="BT2"/> <Button android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Show External Storage"Android:onclick="BT3"/></linearlayout>

Java code

 PackageCom.hanqi.cunchu;ImportAndroid.content.res.AssetManager;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast;ImportJava.io.FileOutputStream;ImportJava.io.InputStream; Public classLianxiextendsappcompatactivity {ImageView IV, IV2; intI=0; byte[]b=New byte[1024]; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.lianxi); IV=(ImageView) Findviewbyid (R.ID.IV); Iv2=(ImageView) Findviewbyid (R.ID.IV2); }     Public voidbt (View v) {Try{Assetmanager Assetmanager=getassets (); InputStream InputStream= Assetmanager.open ("Aa.jpg"); String Path=getexternalfilesdir ("Music"). toString (); Toast.maketext (Lianxi. This, Path, Toast.length_long). Show (); FileOutputStream FOS1=openfileoutput ("B.jpg", mode_private); FileOutputStream Fos=NewFileOutputStream (path+ "A.jpg");  while((I=inputstream.read (b)) >0) {Fos1.write (b,0, i); Fos.write (b,0, i);            } fos1.close ();            Fos.close ();            Inputstream.close (); String path1=getfilesdir (). Getcanonicalpath () + "/b.jpg"; Bitmap b=Bitmapfactory.decodefile (path1);        Iv.setimagebitmap (b); }Catch(Exception e) {e.printstacktrace (); }    }     Public voidbt2 (view view) {return;//if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//try{//String path=getexternalfilesdir ("Music"). toString ();//toast.maketext (lianxi.this, Path, Toast.length_long). Show ();////fileoutputstream fos=new FileOutputStream (path+ "a.jpg");//printstream ps=new printstream (FOS);//string S=new string (b,0,i);//Ps.print (s);//ps.close ();//fos.close ();//Toast.maketext (Lianxi.this, "write complete", Toast.length_short). Show ();////}catch (Exception e) {//e.printstacktrace ();//            }//        }//else{//Toast.maketext (lianxi.this, "No SD card", Toast.length_short). Show ();//        }    }     Public  voidBT3 (View v) {Try{String path=getexternalfilesdir ("Music"). Getcanonicalpath () + "a.jpg"; Bitmap b=bitmapfactory.decodefile (path);        Iv2.setimagebitmap (b); }        Catch(Exception e) {e.printstacktrace (); }    }}

Assets go to internal and external storage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.