1

MediaStore 内のすべてのビデオを一覧表示するアプリケーションがあります。(内部ストレージと外部ストレージの両方が必要です)。

基本的に、MediaStore.Video.Media.EXTERNAL_CONTENT_URI と MediaStore.Video.Media.INTERNAL_CONTENT_URI を照会する 2 つのカーソルがあります。

次に、MergeCursor を使用してこれらのクエリをマージし、CursorAdapter を使用して ListView に表示します。

問題は、動画の 1 つを削除したい場合があることですが、選択した動画のストレージを特定する方法がないため、この操作には「コンテンツ URI」が必要です。

動画の完全なファイル パスと MediaStore の ID を取得しています。

ファイルパス/Uriから「コンテンツUri」を取得するにはどうすればよいですか?

4

2 に答える 2

4

タイトルの質問に答えるには:

URI からパスを取得し、アプリのパスから URI を取得する必要があります。前者:

/**
 * Gets the corresponding path to a file from the given content:// URI
 * @param selectedVideoUri The content:// URI to find the file path from
 * @param contentResolver The content resolver to use to perform the query.
 * @return the file path as a string
 */
private String getFilePathFromContentUri(Uri selectedVideoUri,
        ContentResolver contentResolver) {
    String filePath;
    String[] filePathColumn = {MediaColumns.DATA};

    Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    filePath = cursor.getString(columnIndex);
    cursor.close();
    return filePath;
}

後者 (これはビデオに対して行いますが、MediaStore.Video を MediaStore.Audio (など) に置き換えることで、オーディオやファイル、またはその他の種類の保存されたコンテンツにも使用できます):

/**
 * Gets the MediaStore video ID of a given file on external storage
 * @param filePath The path (on external storage) of the file to resolve the ID of
 * @param contentResolver The content resolver to use to perform the query.
 * @return the video ID as a long
 */
private long getVideoIdFromFilePath(String filePath,
        ContentResolver contentResolver) {


    long videoId;
    Log.d(TAG,"Loading file " + filePath);

            // This returns us content://media/external/videos/media (or something like that)
            // I pass in "external" because that's the MediaStore's name for the external
            // storage on my device (the other possibility is "internal")
    Uri videosUri = MediaStore.Video.Media.getContentUri("external");

    Log.d(TAG,"videosUri = " + videosUri.toString());

    String[] projection = {MediaStore.Video.VideoColumns._ID};

    // TODO This will break if we have no matching item in the MediaStore.
    Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(projection[0]);
    videoId = cursor.getLong(columnIndex);

    Log.d(TAG,"Video ID is " + videoId);
    cursor.close();
    return videoId;
}

基本的に、(または照会しているそのサブセクションの)DATA列にはファイル パスが格納されるため、その情報を使用して検索します。MediaStore

于 2012-07-22T20:49:09.833 に答える
1

自分の質問に答える:)

パスのストレージを見つけるための簡単で簡単な (!) 方法を見つけました:**

    if(selectedVideoPath.indexOf(Environment.getExternalStorageDirectory().getPath()) == 0)
    {
        getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
                MediaStore.Video.Media._ID + "=" + videoId,
                null);
    }
    else {
        getContentResolver().delete(MediaStore.Video.Media.INTERNAL_CONTENT_URI, 
                MediaStore.Video.Media._ID + "=" + videoId,
                null);          
    }
于 2011-02-18T20:47:03.080 に答える