Question Detail

Error saying that you have cursor on 0th index in 0length of array and 
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
 

I am getting this error while trying to open the image in android in below Method : 

//method to get the file path from uri
public String getPath(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    String document_id = cursor.getString(0);
    document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
    cursor.close();

    cursor = getContentResolver().query(
            MediaStore.Images.Media.INTERNAL_CONTENT_URI,
            null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
    cursor.moveToFirst();
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
    cursor.close();

    return path;
}




E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.vbagetech, PID: 30071

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)

at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:147)

at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:61)

at android.database.CursorWrapper.getString(CursorWrapper.java:114)

at com.vbagetech.upload.test.UploadFilesActivity.getPath(UploadFilesActivity.java:137)

at com.vbagetech.upload.test.UploadFilesActivity.uploadMultipart(UploadFilesActivity.java:80)

at com.vbagetech.upload.test.UploadFilesActivity.onClick(UploadFilesActivity.java:184)

at android.view.View.performClick(View.java:4467)

at android.view.View$PerformClick.run(View.java:18776)

at android.os.Handler.handleCallback(Handler.java:808)

at android.os.Handler.dispatchMessage(Handler.java:103)

at android.os.Looper.loop(Looper.java:193)

at android.app.ActivityThread.main(ActivityThread.java:5292)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:515)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)

at dalvik.system.NativeStart.main(Native Method)

 


Thread Reply