1. 程式人生 > >Android 外部儲存,內部儲存路徑

Android 外部儲存,內部儲存路徑

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class DirectoryUtils {
   private static final String TAG = "DirectoryUtils";
   
   public static void getEnvironmentDirectories() {
      //:/system
      Log.i(TAG, "Environment.getRootDirectory==" +
            Environment.getRootDirectory().toString());
      
      //:/data 使用者資料目錄
      Log.i(TAG, "Environment.getDataDirectory==" +
            Environment.getDataDirectory().toString());
      
      //:/cache 下載快取內容目錄
      Log.i(TAG, "Environment.getDownloadCacheDirectory==" +
            Environment.getDownloadCacheDirectory().toString());
      
      //:/mnt/sdcard或者/storage/emulated/0或者/storage/sdcard0 主要的外部儲存目錄
      
      //這個不一定是外部儲存
      Log.i(TAG, "Environment.getExternalStorageDirectory==" +
            Environment.getExternalStorageDirectory().toString());
      
      //:/mnt/sdcard/Pictures或者/storage/emulated/0/Pictures或者/storage/sdcard0/Pictures
      Log.i(TAG, "Environment.getExternalStoragePublicDirectory==" +
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString());
      
      //獲取SD卡是否存在:mounted
      Log.i(TAG, "Environment.getExternalStorageState==" +
            Environment.getExternalStorageState().toLowerCase());
      
      //裝置的外存是否是用記憶體模擬的,是則返回true。(API Level 11)
      Log.i(TAG, "Environment.isExternalStorageEmulated==" +
            Environment.isExternalStorageEmulated());
      
      //裝置的外存是否是可以拆卸的,比如SD卡,是則返回true。(API Level 9)
      Log.i(TAG, "Environment.isExternalStorageRemovable==" +
            Environment.isExternalStorageRemovable());
   }
   
   public static void getApplicationDirectories(Context context) {
      
      //獲取當前程式路徑 應用在記憶體上的目錄 :/data/data/com.example.demo/files
      Log.i(TAG, "context.getFilesDir==" +
            context.getFilesDir().toString());
      
      //應用的在記憶體上的快取目錄 :/data/data/com.example.demo/cache
      Log.i(TAG, "context.getCacheDir==" +
            context.getCacheDir().toString());
      
      //應用在外部儲存上的目錄 :/storage/emulated/0/Android/data/com.example.demo/files/Movies
      Log.i(TAG, "context.getExternalFilesDir==" +
            context.getExternalFilesDir(Environment.DIRECTORY_MOVIES).toString());
      
      //應用的在外部儲存上的快取目錄 :/storage/emulated/0/Android/data/com.example.demo/cache
      Log.i(TAG, "context.getExternalCacheDir==" +
            context.getExternalCacheDir().toString());
      
      //獲取該程式的安裝包路徑 :/data/app/com.example.zhaohw.demo-1/base.apk
      Log.i(TAG, "context.getPackageResourcePath==" +
            context.getPackageResourcePath());
      
      //獲取程式預設資料庫路徑 :/data/data/com.example.demo/databases/mufeng
      Log.i(TAG, "context.getDatabasePath(\"zhaohw\")==" +
            context.getDatabasePath("zhaohw").toString());
   }
}