在将图片用 file.renameTo(file)
它们在图库中变得灰暗,无法使用。我试过了所有类型的意图来重新扫描图像,但最终没有希望,有些在旧版本上工作,有些根本就不工作,所以我试图更新 MediaStore
但不知道怎么做才对。
下面是我的一些代码
这是我早期的一个方法
MediaScannerConnection.scanFile(mContext,
filesPath , null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
另一个
private void scanMedia(File file) {
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
mContext.sendBroadcast(scanFileIntent);
}
这工作在19年前,但不是在kitkat的工作
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
这是MediaStore的一个
int row = mContext.getContentResolver()
.update(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
valuesNewFilePath,
MediaStore.MediaColumns.DATA + "='" + oldFilePath + "'",
null);
这个方法在logcat中抛出这个不致命的异常。
07-04 02:37:18.448: WMediaProvider(2947): java.lang.NumberFormatException: Invalid long: "media" 07-04 02:37:18.448: WMediaProvider(2947): at java.lang.Long.invalidLong(Long.java:124) 07-04 02:37:18.448: WMediaProvider(2947): at java. WMediaProvider(2947): at java.lang.Long.parse(Long.java:361) 07-04 02:37:18.448: WMediaProvider(2947): at java.lang.Long.parseLong(Long.java:352) 07-04 02:37:18. 448: WMediaProvider(2947): at java.lang.Long.parseLong(Long.java:318) 07-04 02:37:18.448: WMediaProvider(2947): at android.content.ContentUris. parseId(ContentUris.java:86) 07-04 02:37:18.448: WMediaProvider(2947): at com.android.provider.media.MediaThumbRequest.(MediaThumbRequest.java:93) 07-04 02:37:18. 448: WMediaProvider(2947): at com.android.provider.media.MediaProvider.requestMediaThumbnail(MediaProvider.java:3742) 07-04 02:37:18.448: WMediaProvider(2947): at com.android.provider.media.MediaProvider.update(MediaProvider.java:4353) 07-04 02:37:18.448: WMediaProvider(2947): at android. Content.ContentProvider$Transport.update(ContentProvider.java:287) 07-04 02:37:18.448: WMediaProvider(2947): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:215) 07-04 02:37:18.448: WMediaProvider(2947): at android.os.Binder.execTransact(Binder.java:404)
EDIT: 我相信这是能做到的,因为当我重命名或添加一些媒体文件,然后使用ES文件资源管理器并改变该媒体包含文件夹的路径时,它们会出现在图库中并被添加到媒体存储中,如果重新生成缩略图需要一些时间。因为当我重命名或添加一些媒体文件,然后使用ES文件资源管理器和改变路径的媒体包含文件夹,他们得到出现在画廊和添加到媒体存储,它需要一些时间来重新生成缩略图,如果你有太多的媒体在该文件夹上。Media Provider object removed
然后一些日志猫与标签 art
关于装箱位图,我相信唯一的方法就是用新的数据和位图更新MediaStore。现在如何找到并删除以前的数据?MediaStore
并添加新的?如果我把它从MediaStore删除它是导致删除该文件从磁盘?
先谢谢你
这个方法对我来说是工作在Android-21。
public static boolean renameImageFile(Context c, File from, File to) {
to = getFileWithUnicName(to);
if (from.renameTo(to)) {
removeMedia(c, from);
addMedia(c, to);
return true;
} else {
return false;
}
}
public static void addMedia(Context c, File f) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(f));
c.sendBroadcast(intent);
}
private static void removeMedia(Context c, File f) {
ContentResolver resolver = c.getContentResolver();
resolver.delete(Images.Media.EXTERNAL_CONTENT_URI, Images.Media.DATA + "=?", new String[] { f.getAbsolutePath() });
}
而不是像你上次尝试的那样,直接更新mediastore中的图片文件,你应该先尝试删除旧行,然后添加新行。
告诉我是否解决了你的问题。