Internal storage, external storage of Android data storage

Source: Internet
Author: User

First of all, what is internal storage ?

Under the Android platform, it has its own independent data storage rules, under the Windows platform, the application can access or modify the file resources under other applications freely or on the basis of specific access rights.

But under the Android platform, all the data of an application is private, and only the application itself can access it.

When the application is installed on the system, the package in which it resides will have a folder for its own data, and only this application can write permissions to the folder, which is located in the Android/data/data directory.

Reading internal data can use the Openfileoutput method and the Openfileinput () method, which mainly writes data to an internal data file, which reads data from an internal data file.

Specific examples are as follows:

The first is to write the data:

try {//Get target file from internal data fileoutputstream fileoutput = openfileoutput ("Internal data file name", context.mode_private); try {// Packing scale OutputStreamWriter class, easy to write outputstreamwriter OSW = new OutputStreamWriter (Fileoutput, "set character set"); try {//write data Osw.write (New String ("Asdfasfs")); catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (Unsupportedencodingexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}

Next is to read the data

try {//Get target file from internal data fileinputstream fileinput = openfileinput ("filename");//wrapper, easy to read InputStreamReader isr;try {ISR = New InputStreamReader (Fileinput, "character set");//create a character array to hold the contents of the read try {isr.read (new char[512]);} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();}} catch (Unsupportedencodingexception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}

The above is the internal data reading, if the picture and other data should be encoded separately.

With internal data, how do you read external data?

The first reading of external data requires establishing permissions in the manifest file:

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android: Name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Get permission to operate external storage, which is our SD card

Obtaining an SD card requires the use of the Environment class environment.

Gets the file location of the externally stored files SDcard = Environment.getexternalstoragedirectory ();

It is best to note that the SD card before acquiring the file directory status, according to the API documentation, SD card status constants indicate the current status of the SD card, such as non-readable, non-existent, see the API documentation. Get the status of the SD card as follows:

Environment.getexternalstoragestate ()

It returns a String object, so we can perform subsequent operations after comparing the SD card status constants with the equal method.

The read operation of the SD card is as follows:

Message response to read Data Btnread.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated Method Stubif (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {File Sdcardfile = new File (Environment.getexternalstoragedirectory (), "myNewFile.txt");//First determine if the SD card file exists if ( Sdcardfile.exists ()) {//detection can read if (Sdcardfile.canread ()) {try {fileinputstream input = new FileInputStream (sdcardfile ); byte[] buffer = new Byte[512];try {input.read (buffer); Tv_show.settext (new String (buffer, "GB2312");} catch ( IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} Else{toast.maketext (Getapplicationcontext (), "SD Card not detected", Toast.length_short). Show (); return;}}});

Write to SD card:

Message response to write Data Btnwrite.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method stub//Gets the file location of the external store files SDcard = Environment.getexternalstoragedirectory ();//new File NewFile = New File (SDcard, "MyNewFile.txt"), if (!sdcard.exists ()) {Toast.maketext (Getapplicationcontext (), "Current SD card path does not exist", Toast.length_short). Show (); return;} Else{try {//create if (newfile.exists ()) {Toast.maketext (Getapplicationcontext (), "File already exists" in my phone's storage folder, Toast.length_ Short). Show (); Newfile.delete (); return;} Newfile.createnewfile (); Toast.maketext (Getapplicationcontext (), "Create Complete", Toast.length_short). Show (); FileOutputStream outputstream = new FileOutputStream (newfile); OutputStreamWriter writer = new OutputStreamWriter ( OutputStream, "GB2312"), Writer.write (Ed_text.gettext (). toString ()); Writer.flush (); Writer.close (); O Utputstream.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}});

The following is a brief introduction to how to obtain the current storage status of the SD card:

The class used is the Statfs class, which can be obtained from the file object's GetPath ():

Gets the storage status of the SD card file SDcard = Environment.getexternalstoragedirectory (); StatFs SFS = new StatFs (Sdcard.getpath ());//Gets the overall capacity long total = Sfs.gettotalbytes ();//Gets the current capacity long free = sfs.getfreebytes ( ); TextView TV_SD = (TextView) Findviewbyid (r.id.tv_sd_storage); Tv_sd.settext ("SD also remaining:" +free/1024/1024+ "MB/" +total/ 1024/1024+ "MB"); ProgressBar progress = (ProgressBar) Findviewbyid (r.id.progress); float num = ((float) (free/1024/1024)/(float) (total/ 1024/1024)) *100;int progress1 = (int) num;progress.setprogress (PROGRESS1);


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Internal storage, external storage of Android data 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.