android--data storage: SD card storage for external storage of mobile phones

Source: Internet
Author: User

Xml

 <EditTextAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/et_5"Android:hint= "What to store"/>    <EditTextAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/et_6"Android:hint= "Content read from file"        />    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal">        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:text= "Save to Directory with package name"Android:layout_weight= "1"Android:onclick= "Onclick7"/>        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:text= "Get from directory with package name"Android:layout_weight= "1"Android:onclick= "Onclick8"/>    </LinearLayout>    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal">        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:text= "Save to a custom directory"Android:layout_weight= "1"Android:onclick= "Onclick9"/>        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:text= "Getting from a custom directory"Android:layout_weight= "1"Android:onclick= "Onclick10"/>    </LinearLayout>

Java

   //save files to the directory with package names to the external space store     Public voidonclick7 (view view) {//        if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//1. Get the content you want to storeString content =Et_5.gettext (). toString (); //2. Get the external storage directory with package name--only to the root directory//String Sdpath = Environment.getexternalstoragedirectory (). GetAbsolutePath (); //Toast.maketext (activitydata.this, "sdpath=" +sdpath, Toast.length_long). Show (); //the parameter represents a subdirectory of a different file type if it is not passed null gets the directory with the package name under the root directoryString Sdpath = Getexternalfilesdir (NULL). GetAbsolutePath (); Toast.maketext (activitydata. This, "sdpath=" +Sdpath, Toast.length_long). Show (); //3. Constructing the output streamSdpath + = "/" +FILENAME; Try{FileOutputStream fos=NewFileOutputStream (Sdpath,true); //traditional Way byte array modeFos.write (Content.getbytes ("Utf-8"));                Fos.close (); Toast.maketext (activitydata. This, "Saved successfully", Toast.length_short). Show (); } Catch(Exception e) {e.printstacktrace (); Toast.maketext (activitydata. This, "Save Failed", Toast.length_short). Show (); }        }        Else{toast.maketext (activitydata). This, "SD card is not mounted", Toast.length_short). Show (); }    }    //Read the package name file from the external storage space//determine if the mount is mounted     Public voidonclick8 (view view) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//1. Get the content you want to store//String content = Et_5.gettext (). toString (); //2. Get the external storage directory with package name--only to the root directory//String Sdpath = Environment.getexternalstoragedirectory (). GetAbsolutePath (); //Toast.maketext (activitydata.this, "sdpath=" +sdpath, Toast.length_long). Show (); //the parameter represents a subdirectory of a different file type if it is not passed null gets the directory with the package name under the root directoryString Sdpath = Getexternalfilesdir (NULL). GetAbsolutePath (); //Toast.maketext (activitydata.this, "sdpath=" +sdpath, Toast.length_long). Show (); //3. Constructing the input streamSdpath + = "/" +FILENAME; Try{FileInputStream fis=NewFileInputStream (Sdpath); byte[] B =New byte[1024]; inti = 0; StringBuilder SBR=NewStringBuilder ();  while((i = Fis.read (b)) >0) {sbr.append (NewString (b,0, i));                Et_6.settext (SBR);                } fis.close (); Toast.maketext (activitydata. This, "Get Success", Toast.length_short). Show (); } Catch(Exception e) {e.printstacktrace (); Toast.maketext (activitydata. This, "Get Failed", Toast.length_short). Show (); }        }        Else{toast.maketext (activitydata). This, "SD card is not mounted", Toast.length_short). Show (); }    }    //saving files to a custom directory     Public voidOnclick9 (view view) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//1. Get the content you want to storeString content =Et_5.gettext (). toString (); //2. Get the root directory for external storageString Sdpath =environment.getexternalstoragedirectory (). GetAbsolutePath (); //Create subdirectories in the root directory of the SD cardSdpath + = "/hanqi"; //instantiate file, point to a directoryFile File =NewFile (Sdpath); //If it does not exist, create a            if(!file.exists ()) {                //Create a directoryFile.mkdirs (); } toast.maketext (Activitydata. This, "path=" +Sdpath, Toast.length_short). Show (); //3. Create an output streamSdpath + = "/" +FILENAME; Try{FileOutputStream fos=NewFileOutputStream (Sdpath,true); //traditional Way byte array modeFos.write (Content.getbytes ("Utf-8"));                    Fos.close (); Toast.maketext (activitydata. This, "Saved successfully", Toast.length_short). Show (); } Catch(Exception e) {e.printstacktrace (); Toast.maketext (activitydata. This, "Save Failed", Toast.length_short). Show (); }            }        Else{toast.maketext (activitydata). This, "SD card is not mounted", Toast.length_short). Show (); }    }     Public voidonclick10 (view view) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//1. Get the content you want to store//String content = Et_5.gettext (). toString (); //2. Get the external storage directory with package name--only to the root directoryString Sdpath =environment.getexternalstoragedirectory (). GetAbsolutePath (); //Toast.maketext (activitydata.this, "sdpath=" +sdpath, Toast.length_long). Show (); //the parameter represents a subdirectory of a different file type if it is not passed null gets the directory with the package name under the root directory//String sdpath = Getexternalfilesdir (null). GetAbsolutePath (); //Toast.maketext (activitydata.this, "sdpath=" +sdpath, Toast.length_long). Show (); //3. Constructing the input streamSdpath + = "/hanqi/" +FILENAME; Toast.maketext (activitydata. This, ""+Sdpath, Toast.length_short). Show (); Try{FileInputStream fis=NewFileInputStream (Sdpath); byte[] B =New byte[1024]; inti = 0; StringBuilder SBR=NewStringBuilder ();  while((i = Fis.read (b)) >0) {sbr.append (NewString (b,0, i));                Et_6.settext (SBR);                } fis.close (); Toast.maketext (activitydata. This, "Get Success", Toast.length_short). Show (); } Catch(Exception e) {e.printstacktrace (); Toast.maketext (activitydata. This, "Get Failed", Toast.length_short). Show (); }        }        Else{toast.maketext (activitydata). This, "SD card is not mounted", Toast.length_short). Show (); }    }

android--data storage: SD card storage for external phone 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.