尝试从SQLite数据库加载图像时出错

时间:2014-12-24 14:13:01

标签: android sqlite

我已经将我的图像保存为SQlite数据库的blob(或者我认为它保存了它),再次加载活动时它会因此错误而崩溃:

12-24 16:19:48.679: E/AndroidRuntime(22984): FATAL EXCEPTION: main
12-24 16:19:48.679: E/AndroidRuntime(22984): Process: com.example.main, PID: 22984
12-24 16:19:48.679: E/AndroidRuntime(22984): android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:44)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.database.CursorWrapper.getBlob(CursorWrapper.java:122)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at com.example.main.Map.onLoadFinished(KrugerParkMap.java:341)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at com.example.main.Map.onLoadFinished(KrugerParkMap.java:1)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.content.Loader.deliverResult(Loader.java:104)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.os.Looper.loop(Looper.java:136)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at android.app.ActivityThread.main(ActivityThread.java:5586)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at java.lang.reflect.Method.invokeNative(Native Method)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at java.lang.reflect.Method.invoke(Method.java:515)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
12-24 16:19:48.679: E/AndroidRuntime(22984):    at dalvik.system.NativeStart.main(Native Method)

好的,这就是我如何保存图片:

contentValues.put(LocationsDB.FIELD_IMAGE, imageData);

....

baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();

然后加载图片时:

public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    int locationCount = 0;
    double lat=0;
    double lng=0;
    float zoom=0;
    locationCount = arg1.getCount();
    arg1.moveToFirst();

    for(int i=0;i<locationCount;i++){

        // Get the latitude
        lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
        lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
        zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
        arg1.getBlob(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
        thePoint = new LatLng(lat, lng);
        drawMarker(thePoint);

    }

    if(locationCount>0){
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
            arg1.moveToNext();
            ImageView image = (ImageView) findViewById(R.id.marker_icon);
            imageData = arg1.getBlob(arg1.getColumnIndex("image"));
            image.setImageBitmap(BitmapFactory.decodeByteArray(imageData, 0, imageData.length));
    }
    }

我在这里做错了什么?

修改

我进一步查看了错误代码,并在第331页imageData = arg1.getBlob(arg1.getColumnIndex("image"));

看到了

1 个答案:

答案 0 :(得分:0)

您必须通过调用moveToNext()

向前移动光标
if(arg1.getCount() >= 1){
  while(arg1.moveToNext()){ // Iterate like this over the cursor

        // Get the latitude
        lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
        lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
        zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
        arg1.getBlob(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
        thePoint = new LatLng(lat, lng);
        drawMarker(thePoint);

}
if (arg1 != null && !arg1.isClosed()){
    arg1.close();
}

请注意,arg1是Cursor