我试图用此代码删除我的视频,但在某些设备上,它们显示为无法在图库中播放的损坏视频。如何从图库中删除视频?
File videoFile = new File(filepath);
videoFile.delete();
如何正确删除它们?
我试图用此代码删除我的视频,但在某些设备上,它们显示为无法在图库中播放的损坏视频。如何从图库中删除视频?
File videoFile = new File(filepath);
videoFile.delete();
如何正确删除它们?
Java代码似乎是正确的,你有没有在你的清单中声明WRITE_EXTERNAL_STORAGE
? 如果不是,请在<application>
标记外面添加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
图库正在显示媒体库数据。要从图库中完全删除文件,您必须删除视频的mediastore数据库行。
在代码中从媒体存储中删除记录。
// delete the mediastore entry;
getContext().getContentResolver().delete(mediaStoreUri, null, null);
现在,所有你需要的是mediaStoreUri你可以从文件路径得到它。
//found this on github
https://gist.github.com/jdeloach/3172742
问题与上面的链接我宁愿他们使用的ContentUris.withAppendedId静态方法来得到URI,而是他们只是添加斜线和字符串出来
Uri mediaStoreUri = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
ImageId);
基础的对danny117的回答我找到了删除视频库的解决方案:
/**
* Returns the Uri which can be used to delete/work with images in the photo gallery.
* @param filePath Path to IMAGE on SD card
* @return Uri in the format of... content://media/external/images/media/[NUMBER]
*/
private Uri getUriFromPath(String filePath) {
long videoId;
Uri videoUri = MediaStore.Video.Media.getContentUri("external");
String[] projection = {MediaStore.Video.Media._ID};
// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = getContentResolver().query(videoUri, projection, MediaStore.Video.Media.DATA + " LIKE ?", new String[] { filePath }, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
cursor.close();
return contentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoUri);
}
该文件实际上被删除了,所以我已经在manifest文件中设置了权限。 – user2737037 2014-09-23 05:35:27