gpt4 book ai didi

android - 删除图像文件后刷新图库?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:15 25 4
gpt4 key购买 nike

我的问题总能找到以下答案:

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));

但它不适用于我的系统(Nexus4 Android 4. ...)

我可以创建一个文件并将其添加到带有此代码的 Media-DB

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);

"file"是我要添加的新图像文件。

删除文件后,我尝试通过以下方式刷新图库

Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
Uri contentUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
intent.setData(contentUri);
context.sendBroadcast(intent);

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));

但画廊中仍然有空的占位符。

我不知道为什么?...

为了安全起见,我也在 AndroidManifest.xml 中添加了我的 Activity

<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>

但结果是一样的。有解决问题的想法吗?

最佳答案

在 KitKat 之后,您无法发送 Intent 以在整个设备的存储上运行 MediaScanner,因为这是一个 CPU I\O 密集型任务,如果每个应用程序下载图像或删除第一,称 Intent 电池很容易耗尽,因此他们决定阻止该操作。以下是您的选择:

对 pre-KitKat 使用旧方法

传递你的文件路径:

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
} else{


MediaScannerConnection.scanFile(mContext, filePath, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
* (non-Javadoc)
* @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
*/
public void onScanCompleted(String path, Uri uri)
{
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});

}

更可靠的方法是直接更新MediaStore:

// Set up the projection (we only need the ID)
String[] projection = { MediaStore.Images.Media._ID };

// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[] { file.getAbsolutePath() };

// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst()) {
// We found the ID. Deleting the item via the content provider will also remove the file
long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
contentResolver.delete(deleteUri, null, null);
} else {
// File not found in media store DB
}
c.close();

关于android - 删除图像文件后刷新图库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com