測試代碼1: 輸出結果(myapplication為包名): Environment.getDataDirectory():/dataEnvironment.getExternalStorageDirectory():/storage/emulated/0 Environment.getRootDi ...
測試代碼1:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.util.Log.e("wenqiao", "Environment.getDataDirectory():" + android.os.Environment.getDataDirectory().getAbsolutePath() + "\n");
android.util.Log.e("wenqiao", "Environment.getExternalStorageDirectory():" + android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "\n");
android.util.Log.e("wenqiao", "Environment.getRootDirectory():" + android.os.Environment.getRootDirectory().getAbsolutePath() + "\n");
android.util.Log.e("wenqiao", "Environment.getDownloadCacheDirectory():" + android.os.Environment.getDownloadCacheDirectory().getAbsolutePath());
/**
* 這個方法接收一個參數,表明目錄所放的文件的類型,傳入的參數是Environment類中的DIRECTORY_XXX靜態變數,比如DIRECTORY_DCIM等.
* 註意:傳入的類型參數不能是null,返回的目錄路徑有可能不存在,所以必須在使用之前確認一下,比如使用File.mkdirs創建該路徑。
*/
java.io.File path = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_ALARMS);
if( !path.exists() ){
path.mkdirs();
}
/* /storage/emulated/0/Alarms */
android.util.Log.e("wenqiao", "Environment.getExternalStoragePublicDirectory():" + android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_ALARMS));
/**
* contextWrapper
*/
android.util.Log.e("wenqiao", "getCacheDir():" + this.getCacheDir().getAbsolutePath());
android.util.Log.e("wenqiao", "getFilesDir():" + this.getFilesDir().getAbsolutePath());
android.util.Log.e("wenqiao", "getExternalCacheDir():" + this.getExternalCacheDir().getAbsolutePath());
android.util.Log.e("wenqiao", "getExternalFilesDir():" + this.getExternalFilesDir(android.os.Environment.DIRECTORY_DCIM).getAbsolutePath());
android.util.Log.e("wenqiao", "getPackageResourcePath():" + this.getPackageResourcePath());
android.util.Log.e("wenqiao", "getDir():" + this.getDir("myfile", android.content.Context.MODE_PRIVATE));
}
輸出結果(myapplication為包名):
Environment.getDataDirectory():/data
Environment.getExternalStorageDirectory():/storage/emulated/0
Environment.getRootDirectory():/system
Environment.getDownloadCacheDirectory():/data/cache
Environment.getExternalStoragePublicDirectory():/storage/emulated/0/Alarms
getCacheDir():/data/user/0/com.example.mushr.myapplication/cache
getFilesDir():/data/user/0/com.example.mushr.myapplication/files
getExternalCacheDir():/storage/emulated/0/Android/data/com.example.mushr.myapplication/cache
getExternalFilesDir():/storage/emulated/0/Android/data/com.example.mushr.myapplication/files/DCIM
getPackageResourcePath():/data/app/com.example.mushr.myapplication-1/base.apkgetDir():/data/user/0/com.example.mushr.myapplication/app_myfile
測試代碼2:
public static void getEnvironmentDirectories() {
android.util.Log.e(LOG_TAG, "getRootDirectory(): "
+ android.os.Environment.getRootDirectory().toString());
android.util.Log.e(LOG_TAG, "getDataDirectory(): "
+ android.os.Environment.getDataDirectory().toString());
android.util.Log.e(LOG_TAG, "getDownloadCacheDirectory(): "
+ android.os.Environment.getDownloadCacheDirectory().toString());
android.util.Log.e(LOG_TAG, "getExternalStorageDirectory(): "
+ android.os.Environment.getExternalStorageDirectory().toString());
android.util.Log.e(
LOG_TAG,
"getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES): "
+ android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_PICTURES).toString());
// Log.i(
// LOG_TAG,
// "isExternalStorageEmulated(): "
// + Environment.isExternalStorageEmulated());
//
// Log.i(
// LOG_TAG,
// "isExternalStorageRemovable(): "
// + Environment.isExternalStorageRemovable());
}
public static void getApplicationDirectories(android.content.Context context) {
android.util.Log.e(LOG_TAG, "context.getFilesDir(): "
+ context.getFilesDir().toString());
android.util.Log.e(LOG_TAG, "context.getCacheDir(): "
+ context.getCacheDir().toString());
// methods below will return null if the permissions denied
android.util.Log.e(
LOG_TAG,
"context.getExternalFilesDir(Environment.DIRECTORY_MOVIES): "
+ context
.getExternalFilesDir(android.os.Environment.DIRECTORY_MOVIES));
android.util.Log.e(
LOG_TAG,
"context.getExternalCacheDir(): "
+ context.getExternalCacheDir());
}
測試結果2:
getRootDirectory(): /system
getDataDirectory(): /data
getDownloadCacheDirectory(): /data/cache
getExternalStorageDirectory(): /storage/emulated/0
getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES): /storage/emulated/0/Pictures
context.getFilesDir(): /data/user/0/com.example.mushr.myapplication/files
context.getCacheDir(): /data/user/0/com.example.mushr.myapplication/cache
context.getExternalFilesDir(Environment.DIRECTORY_MOVIES): /storage/emulated/0/Android/data/com.example.mushr.myapplication/files/Movies
context.getExternalCacheDir(): /storage/emulated/0/Android/data/com.example.mushr.myapplication/cache