Package Com.example.lenovo.myapplication;import Android.content.sharedpreferences;import Android.content.res.assetmanager;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.environment;import Android.support.v7.app.appcompatactivity;import Android.os.Bundle;import Android.view.view;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.TextView; Import Android.widget.toast;import java.io.file;import java.io.fileinputstream;import java.io.FileOutputStream; Import Java.io.inputstream;import Java.io.printstream;public class Mainactivity extends Appcompatactivity {EditText et 1; TextView TV1; ImageView Iv1; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et1 = (EditText) Findviewbyid (R.ID.ET1); TV1 = (TextView) Findviewbyid (R.ID.TV1); Iv1 = (ImageView) Findviewbyid (R.ID.IV1); } PublIC void bt_1 (View v) {//1-gets sharedpreferences sharedpreferences sharedpreferences = getsharedpreferences ("A BC ", mode_private); 2-get compiler Sharedpreferences.editor editor = Sharedpreferences.edit (); 3-Use editor to add Data editor.putstring ("A", "abcdfe"); Editor.putlong ("B", 123456); 4-Submit Save Editor.commit (); Toast.maketext (Mainactivity.this, "saved successfully", Toast.length_short). Show (); }//Read public void Bt_2 (View v) {sharedpreferences sharedpreferences = getsharedpreferences ("abc", MODE_PRIV ATE); String s = sharedpreferences.getstring ("b", null); Toast.maketext (Mainactivity.this, "key=b" + "value=" + S, Toast.length_short). Show (); }//write internal file public void Bt_3 (View v) {//write file from memory try {//1-get internal storage file = Getfilesdir (); String path = File.getabsolutepath (); Toast.maketext (Mainactivity.this, "path=" + Path, Toast.length_short). Show (); 2-write file with output stream FileOutputStream FileOutputStream = openfileoutput ("Text.txt", mode_private); 3-Write file contents PrintStream PrintStream = new PrintStream (FileOutputStream); String str = Et1.gettext (). toString (); Printstream.println (str); Printstream.println ("line wrapping"); Printstream.close (); Toast.maketext (Mainactivity.this, "saved successfully", Toast.length_short). Show (); } catch (Exception ex) {Toast.maketext (Mainactivity.this, "Save Failed", Toast.length_short). Show (); }}//public void Bt_4 (View v) {try {//input stream FileInputStream FIS = openfileinput ("t Ext.txt "); 1-definition byte[] byte[] b = new byte[1024]; int i = 0;//Read the length of the data String str1 = ""; 2-Loop read while ((i = Fis.read (b)) > 0) {string str = new string (b, 0, I); STR1 + = str; } Fis.cloSE (); Tv1.settext (STR1); } catch (Exception ex) {}} public void Bt_5 (View v) {try {//Operation assets Directory File// 1-Get Assets Directory Manager Manager Assetmanager Assetmanager = Getassets (); 2-Manipulate the asset directory, read-write//1)-read the file first to memory InputStream inputstream InputStream = Assetmanager.open ("abc.jpg"); 2)-Write file to directory outputstream FileOutputStream fileoutputstream = Openfileoutput ("text.jpg", mode_private); Read First write byte[] b = new byte[1024]; int i = 0; while ((i = Inputstream.read (b)) > 0) {fileoutputstream.write (b, 0, I); } fileoutputstream.close (); Inputstream.close (); Toast.maketext (Mainactivity.this, "Save file Succeeded", Toast.length_short). Show (); } catch (Exception e) {toast.maketext (mainactivity.this, "Save file Error", Toast.length_short). Show (); }} public void Bt_6 (View v) { 3-Get the file path String path = Getfilesdir (). GetAbsolutePath () + "/text.jpg"; Toast.maketext (Mainactivity.this, "path=" + Path, Toast.length_short). Show (); 2-Get Bitmap bitmapfactory.decodefile ("File path") from the images stored internally; Bitmap BM = bitmapfactory.decodefile (path); 1-Set picture view of picture data source Iv1.setimagebitmap (BM); The public void bt_7 (View v) {//1-Determines if the SC card is mounted if (environment.getexternalstoragestate (). Equals (ENVIRONMENT.M edia_mounted)) {//Get text box contents String str = Et1.gettext (). toString (); try {//write//1-construct output stream//1) to get the file path. Get the root directory of the SD card//string path=environment.getexternalstoragedirectory (). Getcanonicalpath (); 2) get the package name corresponding to the directory String path = Getexternalfilesdir ("musics"). Getcanonicalpath (); Toast.maketext (Mainactivity.this, "path=" + Path, Toast.length_short). Show (); 2) Construction FileOutputStream FOS = new FileOutputStream ("path"); PrintStream PrintStream = new PrintStream (FOS); Printstream.print (str); Printstream.close (); Fos.close (); Toast.maketext (Mainactivity.this, "Storage succeeded", Toast.length_short). Show (); } catch (Exception e) {toast.maketext (Mainactivity.this, "Storage Failed", Toast.length_short). Show (); }} else {Toast.maketext (mainactivity.this, "SD not", Toast.length_short). Show (); }} public void Bt_8 (View v) {//1-Determines if the SC card is mounted if (environment.getexternalstoragestate (). Equals (environm Ent. media_mounted) {try {String path = Getexternalfilesdir ("Music"). Getcanonicalpath () + " /text.jpg "; FileInputStream fls=new FileInputStream (path); Byte[]b=new byte[1024]; int i=0; String str= ""; while ((I=fls.read (b)) >0) { Str+=new String (b,0,i); } fls.close (); Toast.maketext (Mainactivity.this, "read successfully, File contents:" +str, Toast.length_short). Show (); } catch (Exception e) {toast.maketext (Mainactivity.this, "read failed", Toast.length_short) . Show (); }} else {Toast.maketext (mainactivity.this, "SD not", Toast.length_short). Show (); } }}
View Code
Xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns: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:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.lenovo.myapplication.MainActivity"android:orientation= "vertical"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Hello world!"Android:id= "@+id/tv1"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "SP Storage"Android:onclick= "Bt_1"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "sp read"Android:onclick= "Bt_2"/> <EditTextAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/et1"Android:hint= "Input ..."/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Write internal file"Android:onclick= "Bt_3"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Read internal file"Android:onclick= "Bt_4"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Save asset file to internal storage"Android:onclick= "Bt_5"/> <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/iv1"android:src= "@drawable/nnn"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Set picture pointing to internal storage"Android:onclick= "Bt_6"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Write to external storage file"Android:onclick= "bt_7"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Read external storage File"Android:onclick= "Bt_8"/></LinearLayout>
View Code
External storage for phone internal storage