Summary of Android file private storage and access to resource files

Source: Internet
Author: User
Tags file copy
Android File Private Storage


First, the internal storage path is/data/data/youpackagename/, and the paths explained below are based on your own application's internal storage path. Files saved in all internal stores are deleted when the user unloads the application.

First, Files

1. Context.getfilesdir (), the method returns the/data/data/youpackagename/files file object.
2. Context.openfileinput () and Context.openfileoutput () can only read and write files under File, and return FileInputStream and FileOutputStream objects.
3. Context.filelist (), returns all the file names under files, and returns the String[] object.
4. Context.deletefile (String), delete files with the specified name under files.

Second, Cache

Context.getcachedir (), which returns the/data/data/youpackagename/cache file object.

Third, Getdir

Getdir (String name, int mode), returns a folder file object with the specified name under/data/data/youpackagename/, and creates a new folder with the specified name if the folder does not exist.

Access to Android private file resource files


Android has its own security model, and you can see the Android development documentation. An userid is assigned when the application is installed, and the UserID match is required when the application is to access other resources such as files. By default, any file created by the application, the database, sharedpreferences should all be private (located in/data/data/your_project/files/) and the rest of the program cannot be accessed. Unless you specify mode_world_readable or mode_world_writeable at creation time, the rest of the program can be accessed correctly. Because the Android file-reading method is secure, Android requires the user ID of the process to be checked when the process opens the file. So you can't open it directly with the Java API, because Java's IO function does not mention this mechanism. There is a problem that the system must already be root, and the file browser gets root permission otherwise the directory is not visible through the file browser.

file access under a private folder (./data/data/com. Package name/files/below, equivalent to the program working directory)

Cannot open program private data directly with Java API, default path is/data/data/your_project/files/, read files from SDcard, First of all, the file through Android-sdk-windowstoolsadb.exe to the local computer on the file copy to sdcard up, Adb.exe push e:/android.txt/sdcard/, can not use Adb.exe Push E:android.txt SDcard, attention Slash, under Linux and Winodws is not the same, the same: the emulator on the file copy to the local computer: ADB pull/sdcard/android.txt e:/, Read paths can be "/sdcard/android.txt" or "Mnt/sdcard/android.txt"

Such as:

FileReader file = new FileReader ("Android.txt");

Here special emphasis on private data! The implication is that if a file or data is not private to the program, it can be accessed directly using the Java IO API if it is not required to be checked by the Android authority. So-called private data is a file or data that is placed only on SDcard,

You can use the Java IO API to directly open the SDcard file.

Such as:

FileReader file = new FileReader ("/sdcard/android.txt");

If you want to open the program's own private files and data, you must use the activity to provide openfileoutput and Openfileinput methods. To create a private file for a program, you must use the Android read-write file method provided by the activity because of permissions requirements.
Such as:

The code is as follows Copy Code
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Org.apache.http.util.EncodingUtils;

public void Writefiledata (String filename,string message) {
try{
FileOutputStream fout = Openfileoutput (FileName, mode_private);
OutputStreamWriter outwriter = new OutputStreamWriter (fout);
byte [] bytes = Message.getbytes ();
Fout.write (bytes);
Fout.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}

public string Readfiledata (string fileName) {
String res= "";
try{
FileInputStream fin = openfileinput (fileName);
InputStreamReader Inreader = new InputStreamReader (FIN);
Or
FileInputStream fin = new FileInputStream (fileName);
int length = fin.available ();
byte [] buffer = new Byte[length];
Fin.read (buffer);
res = encodingutils.getstring (buffer, "UTF-8");
Fin.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
return res;
}



Second, get the file from the Raw folder in resource and read the data (resource file can only read and write, in "program name ResrawFileName.txt")

The code is as follows Copy Code
public string Getfromraw (string fileName) {
String res = "";
try{
InputStream in = Getresources (). Openrawresource (R.raw.test1);
Resources in Testresrawtest1.txt
int length = in.available ();
byte [] buffer = new Byte[length];
In.read (buffer);
According to Bbi.txt encoding type to choose the appropriate encoding, if not adjusted will garbled
res = encodingutils.getstring (buffer, "UTF-8");
Res =encodingutils.getstring (buffer, "BIG5");
Res =encodingutils.getstring (buffer, "UNICODE");
In.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
return res;
}


third, get the file from the asset and read the data (resource file can only read and write, in "program name AssertsFileName.txt")

The code is as follows Copy Code
public string Getfromasset (string fileName) {
String res= "";
try{
InputStream in = Getresources (). Getassets (). open (FileName);
int length = in.available ();
byte [] buffer = new Byte[length];
In.read (buffer);
res = encodingutils.getstring (buffer, "UTF-8");
}
catch (Exception e) {
E.printstacktrace ();
}
return res;
}
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.