将视频缩略图放入arraylist

时间:2013-04-24 17:17:24

标签: android arraylist cursor thumbnails

我正在尝试查询视频缩略图并将其放入arraylist中。但是,当我尝试将其添加到arraylist时,它会出现此错误

04-24 12:09:35.489: D/Checking Database(11505): in the Do while
04-24 12:09:35.494: D/Checking Database(11505): the columnindex is 1
04-24 12:09:35.494: D/AndroidRuntime(11505): Shutting down VM
04-24 12:09:35.494: W/dalvikvm(11505): threadid=1: thread exiting with uncaught exception (group=0x40c351f8)
04-24 12:09:35.504: E/AndroidRuntime(11505): FATAL EXCEPTION: main
04-24 12:09:35.504: E/AndroidRuntime(11505): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.camerapreview/com.example.camerapreview.Gridview}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.os.Looper.loop(Looper.java:137)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.app.ActivityThread.main(ActivityThread.java:4507)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at java.lang.reflect.Method.invoke(Method.java:511)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at dalvik.system.NativeStart.main(Native Method)
04-24 12:09:35.504: E/AndroidRuntime(11505): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.database.CursorWrapper.getString(CursorWrapper.java:114)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at com.example.camerapreview.Gridview.onCreate(Gridview.java:63)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.app.Activity.performCreate(Activity.java:4465)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
04-24 12:09:35.504: E/AndroidRuntime(11505):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
04-24 12:09:35.504: E/AndroidRuntime(11505):    ... 11 more

这是我正在使用的代码。

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



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preview);

GridView gridview = (GridView) this.findViewById(R.id.gridview);

    thumbCursor = this.getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
        thumbColumns,
        null,
        null, null);



    ArrayList<String> thumb = new ArrayList<String>();

            thumbCursor.moveToFirst();

            do {
                Log.d(TAG, "in the Do while");

                int columnindex = thumbCursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);
                Log.d(TAG, "the columnindex is " + columnindex);                

                thumb.add(thumbCursor.getString(columnindex));


            } while (thumbCursor.moveToNext());

非常感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:0)

  

引起:android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0

您的thumbCursor为空,在从光标读取值之前请检查它是否包含任何数据,否则您将获得上述异常。

像这样检查

if( thumbCursor.moveToFirst())
 {
    do {
                    Log.d(TAG, "in the Do while");
                    int columnindex = thumbCursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);
                    Log.d(TAG, "the columnindex is " + columnindex);                
                    thumb.add(thumbCursor.getString(columnindex));
                } while (thumbCursor.moveToNext());
    }