获取视频缩略图时的getContentResolver()。查询问题

时间:2017-08-17 20:11:07

标签: android

情况:我即将使用managequery代码但不推荐使用它,所以我决定使用getContentResolver().query

但我总是在这一行中收到错误:

Cursor thumbCursor = getContentResolver().query(uri, thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + "=" + id, null, null);
08-18 04:17:49.373 20629-20629/com.chill.leoj.burp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.chill.leoj.burp, PID: 20629
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chill.leoj.burp/com.chill.leoj.burp.Videos}: android.database.sqlite.SQLiteException: no such column: video_id (code 1): , while compiling: SELECT _data, video_id FROM files WHERE (video_id=347888)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2348)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:135)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5345)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
                                                                     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
                                                                  Caused by: android.database.sqlite.SQLiteException: no such column: video_id (code 1): , while compiling: SELECT _data, video_id FROM files WHERE (video_id=347888)
                                                                     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:224)
                                                                     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:180)
                                                                     at android.content.ContentProviderProxy.query(ContentProviderNative.java:447)
                                                                     at android.content.ContentResolver.query(ContentResolver.java:478)
                                                                     at android.content.ContentResolver.query(ContentResolver.java:422)
                                                                     at com.chill.leoj.burp.Videos.onCreate(Videos.java:150)
                                                                     at android.app.Activity.performCreate(Activity.java:6012)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410) 
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:135) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5345) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742) 
                                                                     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 

这就是我在我的应用中使用它的方式:

 Uri uri = MediaStore.Files.getContentUri("external");

    String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,
            MediaStore.Video.Thumbnails.VIDEO_ID };

    String[] mediaColumns = { MediaStore.Video.Media._ID,
            MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
            MediaStore.Video.Media.MIME_TYPE };

    Cursor cursor = getContentResolver().query(uri,
            mediaColumns, null, null, null);



    if (cursor.moveToFirst()) {
        do {

            VideoViewInfo newVVI = new VideoViewInfo();
            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID));

            Cursor thumbCursor = getContentResolver().query(uri, thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
                            + "=" + id, null, null);
            if (thumbCursor.moveToFirst()) {
                newVVI.thumbPath = thumbCursor.getString(thumbCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
                Log.v("", newVVI.thumbPath);
            }

            newVVI.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
            newVVI.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
            newVVI.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
            videoRows.add(newVVI);
        } while (cursor.moveToNext());


        cursor.close();
        videoAdapter = new VideoGalleryAdapter(Videos.this, videoRows);

    }

有人可以指出我做错了什么,如果可能的话,如何解决这个问题。非常感谢你提前。

1 个答案:

答案 0 :(得分:1)

您不应在内容Uri中搜索一般文件(MediaStore.Files.getContentUri("external"))中的视频,而应搜索内容Uri for Video Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;,这样就可以了。