Data storage-External file storage for mobile phones

Source: Internet
Author: User

I. Features

1. Store the file in the external storage space (SD card) of the phone

2. Storage of any type of file

3. operation file using IO input/output stream

4. File path

1-SD Card root directory/android/data/package name/files/[file type], after the application uninstall, the data is deleted;

2-SD root directory/After the application is uninstalled, the data will not be deleted at the same time.

5. Need to declare permissions

1-android.permission.write_external_storage, writing files;

2-mount_unmount_filesystems, create and delete files.

Two. API

1.Environment

1-SD Card Tool Class

2-getexternalstoragestate () Gets the status of the SD card, environment.media_mounted mount status.

3-getexternalstoragedirectory () Get the root directory of the SD card file instance

2.context.getexternalfilesdir (type) to get the external storage path with the package name

Type File types Directory:

1-null indicates that a type subdirectory is not created

2-The corresponding subdirectory is created according to the type

Phone external Memory Storage code display:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context= "Com.hanqi.testapp3.MainActivity" One android:orientation= "vertical"> A  -     <EditText -         Android:layout_width= "Match_parent" the Android:layout_height= "Wrap_content" - Android:hint= "Input ..." - Android:id= "@+id/et_1"/> -  +     <Button -         Android:layout_width= "Match_parent" + Android:layout_height= "Wrap_content" A Android:text= "Write to external storage file" at Android:onclick= "Bt6_onclick"/> -  -     <Button -         Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:text= "Read external storage File" in Android:onclick= "Bt7_onclick"/> -  to  + </LinearLayout>
Phone External Storage
1  Packagecom.hanqi.testapp3;2 3 Importandroid.content.SharedPreferences;4 ImportAndroid.content.res.AssetManager;5 ImportAndroid.graphics.Bitmap;6 Importandroid.graphics.BitmapFactory;7 Importandroid.os.Environment;8 Importandroid.support.v7.app.AppCompatActivity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.text.BidiFormatter; One ImportAndroid.view.View; A ImportAndroid.widget.EditText; - ImportAndroid.widget.ImageView; - ImportAndroid.widget.TextView; the ImportAndroid.widget.Toast; -  - ImportJava.io.File; - ImportJava.io.FileInputStream; + ImportJava.io.FileOutputStream; - ImportJava.io.InputStream; + ImportJava.io.PrintStream; A  at  -  Public classMainactivityextendsappcompatactivity { -  - TextView tv_1; - EditText et_1; - ImageView iv_1; in  - @Override to     protected voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the  *et_1=(EditText) Findviewbyid (r.id.et_1); $ Panax Notoginseng     } -  the  +     //write to external storage file A      Public  voidBt6_onclick (View v) the     { +         //1. Determine if the SD card is mounted -  $         if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) $         { -             //text Box Contents -String str=Et_1.gettext (). toString (); the  -             Try {Wuyi                 //Write the                 //1. Constructing the output stream -                 //1) Get file path Wu  -                 //get the SD card root directory About                 //String path=environment.getexternalstoragedirectory (). Getcanonicalpath (); $  -                 //get the directory that corresponds to the package name -String Path=getexternalfilesdir ("Music"). GetAbsolutePath (); -  AToast.maketext (mainactivity. This, "path=" +path, Toast.length_long). Show (); +                 //2) Construction theFileOutputStream fos =NewFileOutputStream (path+ "/test.txt"); -  $PrintStream ps=NewPrintStream (FOS); the  the ps.print (str); the  the ps.close (); - fos.close (); in  theToast.maketext (mainactivity. This, "Write external file succeeded", Toast.length_short). Show (); the             } About             Catch(Exception e) the             { the e.printstacktrace (); theToast.maketext (mainactivity. This, "Error storing file", Toast.length_short). Show (); +             } -         } the         ElseBayi         { theToast.maketext (mainactivity. This, "SD card is not mounted", Toast.length_short). Show (); the         } -     } -  the      Public voidBt7_onclick (View v) the     { the         //1. Determine if the SD card is mounted the  -         if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) the         { the             Try { the 94String Path = Getexternalfilesdir ("Music"). Getcanonicalpath () + "/test.txt"; the  theFileInputStream fis=NewFileInputStream (path); the 98                 byte[] b=New byte[1024]; About                 intI=0; - 101String str= "";102 103                  while((I=fis.read (b)) >0)104                 { thestr+=NewString (b,0, i);106                 }107 108 fis.close ();109  theToast.maketext (mainactivity. This, "File content =" +str, toast.length_short). Show ();111             } the             Catch(Exception e)113             { the  theToast.maketext (mainactivity. This, "failed to read external file", Toast.length_short). Show (); the             }117         }118         Else119         { -Toast.maketext (mainactivity. This, "SD card is not mounted", Toast.length_short). Show ();121         }122     }123}
external storage of the phone. Java

Data storage-External file storage for mobile phones

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.