簡體   English   中英

如何從圖庫中刪除視頻?

[英]How to delete a video from Gallery?

我嘗試使用此代碼刪除視頻,但在某些設備上,它們顯示為損壞的視頻,無法在“圖庫”中播放。

File videoFile = new File(filepath);
videoFile.delete();

如何正確刪除它們?

圖庫正在顯示媒體存儲數據。 要從圖庫中完全刪除文件,您必須刪除視頻的媒體存儲數據庫行。

  • 在代碼中,從媒體存儲中刪除記錄。

     // 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);
}

Java代碼似乎正確,您WRITE_EXTERNAL_STORAGE在清單中聲明了WRITE_EXTERNAL_STORAGE 如果不是,請在<application>標簽之外添加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM