参考资料一:
android中关于对文件SD卡以及手机本身内存中建立文件保存等操作
http://hi.baidu.com/fql_zlyy/item/855c31bc9ac751f24fc7fde0
在控制台修改手机文件系统的权限
D:\work>cd android-sdk-windows
D:\work\android-sdk-windows>cd platform-tools
D:\work\android-sdk-windows\platform-tools>adb shell
$ su
su
# chmod 755 /data
chmod 755 /data
# chmod 755 /data/data/com.tvie.xj.ivideo/
chmod 755 /data/data/com.tvie.xj.ivideo/
# chmod 755 /data/data/com.tvie.xj.ivideo/files/
chmod 755 /data/data/com.tvie.xj.ivideo/files/
# chmod 755 /data/data/
chmod 755 /data/data/
#
此时没有修改files目录下的文件权限,所以files文件夹下的文件无法读取。
那么如何在程序中修改files文件夹下的文件读取权限呢?
参考链接:修改Android中的文件权限
http://blog.csdn.net/xyylchq/article/details/7817157
在应用管理中点击清除数据时,可以将数据data/data/package-name/中的所有文件(除lib目录外),全部删掉,但是在程序的清除缓存不能成功删除,这是为什么呢
修改前的权限是:- rw- --- ---
修改后的权限是:- rwx r-x r-x
以下是保存二维码的代码片段,如果SD卡不存在,保存到手机上。
// 保存按钮事件
findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(source == null) return;
String title = "userinfo";
long dateTaken = System.currentTimeMillis();
Location location = null;
String filename = "user("+user.getUsername()+").png";
String directory = ImageManager.USER_INFO_DIRECTORY;
// sd卡不存在采用手机卡
if(!checkSDCardExist()) {
directory = context.getFilesDir().getPath();
if(!directory.endsWith("/"))
directory += "/";
}
int[] degree = new int[]{0};
Uri uri = ImageManager.addImage(getContentResolver(), title, dateTaken, location,
directory, filename, source, degree);
if(uri != null)
UIUtils.showToast(context, "保存二维码图片成功");
if(!checkSDCardExist()) {
String command = "chmod 755 " + directory + filename;
runCommand(command);
}
}
});
片段SD卡是否存在的方法:
boolean checkSDCardExist() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
执行chmod命令的方法:
// 修改android文件系统权限
// 示例:runCommand("chmod 777 /data/data/com.tvie.xj.ivideo/files/");
private boolean runCommand(String command) {
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
Logger.i("command", "The Command is : " + command);
process.waitFor();
} catch (Exception e) {
Logger.w("Exception ", "Unexpected error - " + e.getMessage());
return false;
} finally {
try {
process.destroy();
} catch (Exception e) {
Logger.w("Exception ", "Unexpected error - " + e.getMessage());
}
}
return true;
}
删除媒体库代码
// 从content provider中删除p_w_picpath。
public static void deleteImage(ContentResolver cr, String filePath) {
int count = cr.delete(STORAGE_URI, Images.Media.DATA + " = '" + filePath + "'", null);
Logger.i(TAG, "delete record from content where filePath = '"+filePath+"'");
Logger.i(TAG, "count:"+count);
}
其中STORAGE_URI常量:
Uri STORAGE_URI = Images.Media.EXTERNAL_CONTENT_URI;
结果显示删除掉的记录个数是0:
11-11 17:59:38.189: D/ImageManager(22767): delete record from content where filePath = '/mnt/sdcard/ivideo/user/zxing/user(zhenhai01).png'
11-11 17:59:38.189: D/ImageManager(22767): count:0
当android的系统启动的时候,系统会自动扫描sdcard内的多媒体文件,并把获得的信息保存在一个系统数据库中,以后在其他程序中如果想要访问多媒体文件的信息,其实就是在这个数据库中进行的,而不是直接去sdcard中取,理解了这一点以后,问题也随着而来:如果我在开机状态下在sdcard内增加、删除一些多媒体文件,系统会不会自动扫描一次呢?答案是否定的,也就是说,当你改变sdcard内的多媒体文件时,保存多媒体信息的系统数据库文件是不会动态更新的。
详见:android刷新媒体库
http://blog.csdn.net/winson_jason/article/details/8487991
参考资料2:android sdcard文件存储 + 媒体库更新方法
http://hi.baidu.com/moon_2009/item/a1d2cddd493e26dd251f401e
转载于:https://blog.51cto.com/truesea/1323338