[关闭]
@kimo 2016-02-15T02:12:52.000000Z 字数 4666 阅读 1956

SaveFile

android笔记



Choose Internal or External Storage


All Android devices have two file storage areas: "internal" and "external" storage.

Internal storage:

External storage:


Obtain Permissions for External Storage


To write to the external storage, you must request the WRITE_EXTERNAL_STORAGE permission in your manifest file:

  1. <manifest ...>
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  3. ...
  4. </manifest>

Caution: if your app uses the WRITE_EXTERNAL_STORAGE permission, then it implicitly has permission to read the external storage as well.


Save a File on Internal Storage


two methods
getFilesDir()

Returns a File representing an internal directory for your app.

getCacheDir()

Returns a File representing an internal directory for your app's temporary cache files. 

To create a new file in one of these directories, you can use the File() constructor, passing the File provided by one of the above methods that specifies your internal storage directory. For example:

  1. File file = new File(context.getFilesDir(), filename);

Alternatively, you can call openFileOutput() to get a FileOutputStream that writes to a file in your internal directory. For example, here's how to write some text to a file:

  1. String filename = "myfile";
  2. String string = "Hello world!";
  3. FileOutputStream outputStream;
  4. try {
  5. outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  6. outputStream.write(string.getBytes());
  7. outputStream.close();
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }

Or, if you need to cache some files, you should instead use createTempFile(). For example, the following method extracts the file name from a URL and creates a file with that name in your app's internal cache directory:

  1. public File getTempFile(Context context, String url) {
  2. File file;
  3. try {
  4. String fileName = Uri.parse(url).getLastPathSegment();
  5. file = File.createTempFile(fileName, null, context.getCacheDir());
  6. catch (IOException e) {
  7. // Error while creating file
  8. }
  9. return file;
  10. }

Save a File on External Storage


there are two categories of files you might save here:

If you want to save public files on the external storage, use the getExternalStoragePublicDirectory() method to get a File representing the appropriate directory on the external storage. The method takes an argument specifying the type of file you want to save so that they can be logically organized with other public files, such as DIRECTORY_MUSIC or DIRECTORY_PICTURES. For example:

  1. public File getAlbumStorageDir(String albumName) {
  2. // Get the directory for the user's public pictures directory.
  3. File file = new File(Environment.getExternalStoragePublicDirectory(
  4. Environment.DIRECTORY_PICTURES), albumName);
  5. if (!file.mkdirs()) {
  6. Log.e(LOG_TAG, "Directory not created");
  7. }
  8. return file;
  9. }

If you want to save files that are private to your app, you can acquire the appropriate directory by calling getExternalFilesDir() and passing it a name indicating the type of directory you'd like. For example, here's a method you can use to create a directory for an individual photo album:

  1. public File getAlbumStorageDir(Context context, String albumName) {
  2. // Get the directory for the app's private pictures directory.
  3. File file = new File(context.getExternalFilesDir(
  4. Environment.DIRECTORY_PICTURES), albumName);
  5. if (!file.mkdirs()) {
  6. Log.e(LOG_TAG, "Directory not created");
  7. }
  8. return file;
  9. }

Query Free Space


If you know ahead of time how much data you're saving, you can find out whether sufficient space is available without causing an IOException by calling getFreeSpace() or getTotalSpace().


Delete a File


You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call delete() on itself.

myFile.delete();

If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():

myContext.deleteFile(fileName);

Note: When the user uninstalls your app, the Android system deletes the following:

However, you should manually delete all cached files created with getCacheDir() on a regular basis and also regularly delete other files you no longer need.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注