在項(xiàng)目中需要將assets目錄下的文件移動到sd卡下然后訪問,然后在4.0時(shí)可以讀寫文件的內(nèi)容,但是到了5.0和6.0的機(jī)型上就無法讀寫并回顯到界面上,上網(wǎng)搜索發(fā)現(xiàn)在不同的android版本下,獲取的sd卡的緩存路徑是不同的。
在4.0時(shí),當(dāng)你使用getCacheDir().getParent().toString()獲得的路徑是 /data/data/<application package>/cache ,但是當(dāng)你在5.0和6.0版本上使用getCacheDir().getParent().toString()時(shí),得到的是 /data/user/0/<application package>.
開始以為是存儲的路徑不對,存儲時(shí)需要分版本,但是通過log打印發(fā)現(xiàn)存取路徑是能對應(yīng)上的,最終發(fā)現(xiàn)是文件讀取權(quán)限的問題。
這是條目回顯的代碼,開始一直彈toastCan't read from data.user.0.<application package>.config.txt
"所以一直在找路徑問題。
public String getConfigItem(String key) {
assert key != null;
String pkgDirString = getCacheDir().getParent().toString();
String filePath = pkgDirString + File.separator + CONF_FILENAME;//config.txt
String value = null;
try {
value = FileParser.getProfileString(filePath, key);
} catch (IOException e) {
Toast.makeText(this, "Can't read from " + filePath,
Toast.LENGTH_SHORT).show();
}
return value;
}
public static String getProfileString(String file, String key)
throws IOException {
String strLine, value;
BufferedReader br = new BufferedReader(new FileReader(file), 1024);
try {
while ((strLine = br.readLine()) != null) {
strLine = strLine.trim();
strLine = strLine.split("[;#]")[0];
strLine = strLine.trim();
String[] strArray = strLine.split("=");
if (strArray.length == 1) {
value = strArray[0].trim();
if (value.equals(key)) {
value = "";
return value;
}
} else if (strArray.length == 2) {
value = strArray[0].trim();
if (value.equals(key)) {
value = strArray[1].trim();
return value;
}
} else if (strArray.length > 2) {
value = strArray[0].trim();
if (value.equals(key)) {
value = strLine.substring(strLine.indexOf("=") + 1)
.trim();
return value;
}
}
}
} finally {
br.close();
}
return null;
}
后來尋找了好久最終發(fā)現(xiàn)文件根本沒有從assets目錄下轉(zhuǎn)移到sd卡中,于是去那邊的代碼中進(jìn)行尋找。
//獲得android版本
androidVersion = SystemCommands.getAndroidVersion();
SystemCommands.copyFilesFromAssets(getAssets(), androidVersion);
然后是copyFilesFromAssets(getAssets(), androidVersion)的代碼
public static boolean copyFilesFromAssets(AssetManager am,
int androidVersion) {
String certs_dirname = "certs";//證書目錄名
boolean is64cpu = SystemCommands.checkIfCPUx86();
if (androidVersion >= 21) {//5.0以上
if(is64cpu){
String filenames[] = { "config.txt", "vpn-client5-64", "help.pdf","monitorvpn.sh" };
for (String filename : filenames) {
//寫入app文件目錄下
String toString = APP_PATH+ File.separator + filename;
// skip config file except the first time
if (filename == "config.txt" && new File(toString).exists()) {
continue;
}
Log.v(TAG, "copying " + filename + " to " + toString);
copyFileAndChmod(am, filename, toString, "777");
}
}else{
String filenames[] = { "config.txt", "vpn-client5-32", "help.pdf","monitorvpn.sh" };
for (String filename : filenames) {
String toString = APP_PATH + File.separator + filename;
// skip config file except the first time
if (filename == "config.txt" && new File(toString).exists()) {
continue;
}
Log.v(TAG, "copying " + filename + " to " + toString);
copyFileAndChmod(am, filename, toString, "777");
}
}
} else {
String filenames[] = { "config.txt", "vpn-client4", "help.pdf","monitorvpn.sh" };
for (String filename : filenames) {
String toString = APP_PATH + File.separator + filename;
// skip config file except the first time
if (filename == "config.txt" && new File(toString).exists()) {
continue;
}
Log.v(TAG, "copying " + filename + " to " + toString);
copyFileAndChmod(am, filename, toString, "755");
}
}
protected static boolean copyFileAndChmod(AssetManager am, String src_path,
String tgt_path, String mode) {
if (null == mode) {
mode = "777";
}
// Log.v(TAG, "src=" + src_path + ", target=" + tgt_path);
try {
//獲取assets某文件的內(nèi)容。 src_path文件名
InputStream is = am.open(src_path);
//拷貝到的地址
copyFileFromStream(is, tgt_path);
//755 管理者擁有的權(quán)限 與管理者同組的人擁有的權(quán)限 其他人擁有的權(quán)限
//111 101 101 可讀可寫可執(zhí)行 可讀可執(zhí)行 修改文件權(quán)限。
executeCommnad("chmod " + mode + " " + tgt_path);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
//將is內(nèi)容寫到target_path路徑下
public static boolean copyFileFromStream(InputStream is, String target_path) {
try {
FileOutputStream fos = new FileOutputStream(target_path);
int len = 0;
byte[] b = new byte[is.available()];
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.flush();
if (null != is) {
is.close();
}
if (null != fos) {
fos.close();
}
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static String executeCommnad(String args) {
String line, result = "";
try {
Process process;
if (args != null) {
process = Runtime.getRuntime().exec(args);
} else {
process = Runtime.getRuntime().exec("ls");
}
} catch (Throwable t) {
t.printStackTrace();
return null;
}
return "ok";
}
最后把權(quán)限5.0和6.0的權(quán)限改成777就可以正常使用config文件了。問題解決了。。。。。
還有很多人對于存儲路徑很模糊,在這里寫一下。
當(dāng)SD卡存在或者SD卡不可被移除的時(shí)候,就調(diào)用getExternalCacheDir()方法來獲取緩存路徑,否則就調(diào)用getCacheDir()方法來獲取緩存路徑。前者獲取到的就是 /sdcard/Android/data/<application package>/cache 這個(gè)路徑,而后者獲取到的是 /data/data/<application package>/cache 這個(gè)路徑。
public String getDiskCacheDir(Context context) {
String cachePath = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return cachePath;
}
應(yīng)用程序在運(yùn)行的過程中如果需要向手機(jī)上保存數(shù)據(jù),一般是把數(shù)據(jù)保存在SDcard中的。
大部分應(yīng)用是直接在SDCard的根目錄下創(chuàng)建一個(gè)文件夾,然后把數(shù)據(jù)保存在該文件夾中。
這樣當(dāng)該應(yīng)用被卸載后,這些數(shù)據(jù)還保留在SDCard中,留下了垃圾數(shù)據(jù)。
如果你想讓你的應(yīng)用被卸載后,與該應(yīng)用相關(guān)的數(shù)據(jù)也清除掉,該怎么辦呢?
通過Context.getExternalFilesDir()方法可以獲取到 SDCard/Android/data/你的應(yīng)用的包名/files/ 目錄,一般放一些長時(shí)間保存的數(shù)據(jù)
通過Context.getExternalCacheDir()方法可以獲取到 SDCard/Android/data/你的應(yīng)用包名/cache/目錄,一般存放臨時(shí)緩存數(shù)據(jù)
如果使用上面的方法,當(dāng)你的應(yīng)用在被用戶卸載后,SDCard/Android/data/你的應(yīng)用的包名/ 這個(gè)目錄下的所有文件都會被刪除,不會留下垃圾信息。
而且上面二個(gè)目錄分別對應(yīng) 設(shè)置->應(yīng)用->應(yīng)用詳情里面的”清除數(shù)據(jù)“與”清除緩存“選項(xiàng)
如果要保存下載的內(nèi)容,就不要放在以上目錄下。
同時(shí)區(qū)別下面兩個(gè)方法
getCacheDir()方法用于獲取/data/data/<application package>/cache目錄
getFilesDir()方法用于獲取/data/data/<application package>/files目錄
android程序掃描儲存時(shí),如果使用API:Environment.getExternalStorageDirectory().getPath()獲得的是默
可以先判斷下Environment.getExternalStorageDirectory().getParentFile(),如果返回null則沒有父路徑,取Environment.getExternalStorageDirectory().getPath()為當(dāng)前父路徑。
Android開發(fā):filePath放在哪個(gè)文件夾
Environment.getDataDirectory() = /data
Environment.getDownloadCacheDirectory() = /cache
Environment.getExternalStorageDirectory() = /mnt/sdcard
Environment.getExternalStoragePublicDirectory(“test”) = /mnt/sdcard/test
Environment.getRootDirectory() = /system
getPackageCodePath() = /data/app/com.my.app-1.apk
getPackageResourcePath() = /data/app/com.my.app-1.apk
getCacheDir() = /data/data/com.my.app/cache
getDatabasePath(“test”) = /data/data/com.my.app/databases/test
getDir(“test”, Context.MODE_PRIVATE) = /data/data/com.my.app/app_test
getExternalCacheDir() = /mnt/sdcard/Android/data/com.my.app/cache
getExternalFilesDir(“test”) = /mnt/sdcard/Android/data/com.my.app/files/test
getExternalFilesDir(null) = /mnt/sdcard/Android/data/com.my.app/files
getFilesDir() = /data/data/com.my.app/files
參考博客:http://blog.csdn.net/qingzi635533/article/details/51274116