在 android 4.4版本之後 無法任意存取sd card的位置, 只能寫入與存取自己app 下的資料夾
/Android/Data/<app-packagename>/
ex: (/Android/Data/com.example.myapp/)
先利用 getExternalFilesDir(null);
可以自動在內部儲存空間和SD card創立 /Android/Data/<app-packagename>/ 資料夾
因為每個手機的sdcard 路徑都會不同
利用 System.getenv("SECONDARY_STORAGE"); 來取得sd card 的路徑 (ex, /storage/sdcard1/) 最後sdcard的路徑就可以寫成
getExternalFilesDir(null);
String path = System.getenv("SECONDARY_STORAGE") + "/Android/Data/com.example.myapp/";
android 6.0 SD card path
private String androidMarshmallowSDcardPath() {
String rootPath = null;
File f = new File("/storage");
if (f.isDirectory()) {
String[] s = f.list();
for (int i = 0; i < s.length; i++) {
if(s[i].matches(".*-+.*")) {
rootPath ="/storage/" + s[i];
break;
}else if(s[i].matches("exfat_uuid")) {// SONY Z3 SD card path name
rootPath ="/storage/" + s[i];
break;
}
}
}
return rootPath;
}
如果把SD card 格式化成內部儲存空間,例如 HTC M8手機以上有支援此功能
SDcardPath = "/storage/emulated/0/Android/data/com.example.myapp/";//SONY Xperia Miro
if(android.os.Build.MODEL.matches(".*ST23a+.*")) {
SDcardPath = "/mnt/ext_card" +"/Android/data/com.phison.sdcardtest/";
}
相關permission
<uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" />
沒有留言:
張貼留言