apk读取/cache分区内容解决方案
apk读取/cache分区内容
String readFile(String FileName){
String res=null; //存放读取的内容
try{
//打开文件
File file=new File(FileName);
if(!file.exists()){
Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
return null;
}
FileInputStream fin = new FileInputStream(file);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return res;
}
str=readFile("/cache/recovery/test");结果总是提示文件不存在,我已经用adb将文件传递到/cache/recovery目录下了,但是如果文件存放在/data下就可以,是不是因为/cache跨分区了?
该如何使apk可以读取修改/cache/recovery内的文本内容呢
------解决方案--------------------
apk 应该不能直接读取Cache分区的内容的,如果你要内置一个文件,你可以将其内置文件放到assert目录,然后通过assert目录去读取。
------解决方案--------------------
应该是没有权限读取吧,如果实在放文件可以可以放在/storage/sdcard0/下面,这样只需要apk添加读取权限,就可以读取了
String readFile(String FileName){
String res=null; //存放读取的内容
try{
//打开文件
File file=new File(FileName);
if(!file.exists()){
Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
return null;
}
FileInputStream fin = new FileInputStream(file);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
Toast.makeText(this, "读取失败", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return res;
}
str=readFile("/cache/recovery/test");结果总是提示文件不存在,我已经用adb将文件传递到/cache/recovery目录下了,但是如果文件存放在/data下就可以,是不是因为/cache跨分区了?
该如何使apk可以读取修改/cache/recovery内的文本内容呢
------解决方案--------------------
apk 应该不能直接读取Cache分区的内容的,如果你要内置一个文件,你可以将其内置文件放到assert目录,然后通过assert目录去读取。
------解决方案--------------------
应该是没有权限读取吧,如果实在放文件可以可以放在/storage/sdcard0/下面,这样只需要apk添加读取权限,就可以读取了