Layout file:
1 <Button2 Android:layout_width= "Match_parent"3 Android:layout_height= "Wrap_content"4 Android:text= "Save asset file to internal storage"5 Android:onclick= "Bt4_onclick"/>6 <ImageView7 Android:layout_width= "Wrap_content"8 Android:layout_height= "Wrap_content"9 Android:id= "@+id/iv_1"Ten android:src= "@drawable/on"/> One <Button A Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:text= "Set picture pointing to internal storage" the Android:onclick= "Bt5_onclick"/> - <Button - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" + Android:text= "Write to external storage file" - Android:onclick= "Bt6_onclick"/> + <Button A Android:layout_width= "Match_parent" at Android:layout_height= "Wrap_content" - Android:text= "Read external storage File" - Android:onclick= "Bt7_onclick"/>
Java class:
1 //Saving asset files to internal storage2 Public voidBt4_onclick (View v)3 {4 Try {5 //manipulating files in the assets directory6 //1. Get Assetsmanager7Assetmanager am =getassets ();8 //2. Manipulate the asset catalog, read-write side-by- side9 //1) Read file to memory InputStreamTenInputStream is = Am.open ("Yuantu.png"); One //2) Write file to directory OutputStream AFileOutputStream fos = openfileoutput ("Test.png", mode_private); - //Read and write first - byte[] B =New byte[1024]; the inti = 0; - while((i = Is.read (b)) >0) - { -Fos.write (b,0, i); + } - fos.close (); + is.close (); AToast.maketext (mainactivity. This, "Save file succeeded", Toast.length_short). Show (); at } - Catch(Exception e) - { -Toast.maketext (mainactivity. This, "Error saving file", Toast.length_short). Show (); - } - in } - //set a picture to point to internal storage to Public voidBt5_onclick (View v) + { - //1. Get the file path theString path = Getfilesdir (). GetAbsolutePath () + "/test.png"; *Toast.maketext (mainactivity. This, "path =" +path, Toast.length_short). Show (); $ //2. Get Bitmap,bitmapfactory.decodefile ("File path") from the image stored internally;Panax NotoginsengBitmap BM =bitmapfactory.decodefile (path); - //3. Set picture source for picture View the Iv_1.setimagebitmap (BM); + } A //write to external storage file the Public voidBt6_onclick (View v) + { - //1. Determine if the SD card is mounted $ if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) $ { - //Get text Box contents -String str =Et_1.gettext (). toString (); the Try { - //WriteWuyi //1. Constructing the output stream the //1) Get file path - //get the SD card root directory WuString Path =environment.getexternalstoragedirectory (). GetAbsolutePath (); - About //get the directory that corresponds to the package name $ //String Path = Getexternalfilesdir ("Music"). Getcanonicalpath (); -Toast.maketext (mainactivity. This, "path =" +path, Toast.length_long). Show (); - //2) Construction -FileOutputStream fos =NewFileOutputStream (path+ "/test.txt"); APrintStream PS =NewPrintStream (FOS); + ps.print (str); the ps.close (); - fos.close (); $Toast.maketext (mainactivity. This, "Write external file succeeded", Toast.length_short). Show (); the } the Catch(Exception e) the { theToast.maketext (mainactivity. This, "Error storing file", Toast.length_short). Show (); - } in } the Else the { AboutToast.maketext (mainactivity. This, "SD card is not mounted", Toast.length_short). Show (); the } the } the //read external storage files + Public voidBt7_onclick (View v) - { the //1. Determine if the SD card is mountedBayi if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) the { the Try { -String Path = Getexternalfilesdir ("Music"). Getcanonicalpath () + "/test.txt"; -FileInputStream FIS =NewFileInputStream (path); the byte[] B =New byte[1024]; the inti = 0; theString str = ""; the while((i = Fis.read (b)) >0) - { thestr + =NewString (b,0, i); the } the fis.close ();94Toast.maketext (mainactivity. This, "File content =" +str, toast.length_short). Show (); the } the Catch(Exception e) the {98Toast.maketext (mainactivity. This, "failed to read external file", Toast.length_short). Show (); About } - }101 Else102 {103Toast.maketext (mainactivity. This, "SD is not mounted", Toast.length_short). Show ();104 } the}
android--Classroom Arrangement: Assets directory and external storage of mobile phone