Android.database.CursorIndexOutOfBoundsException:请求索引0

时间:2017-12-29 12:55:39

标签: android

当我尝试上传图片时,它会给我下面的错误任务。在编译SDK 26之前它工作正常

  

我正在使用此库上传图片:compile' net.gotev:uploadservice:2.1'我该怎么办???

getPath()方法:

//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.EXTERNAL_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;
}

uploadMultiPart()方法:

public  void uploadMultipart() {

            String path = getPath(filePath);

            //Uploading code
            try {
                String uploadId = UUID.randomUUID().toString();
                //Creating a multi part request
                new MultipartUploadRequest(this, uploadId, AppConfig.RECEIPT_URL)
                        .addFileToUpload(path, "image") //Adding file
                        .addParameter("emp_id", emp_id) //Adding text parameter to the request

                        .setNotificationConfig(new UploadNotificationConfig())
                        .setMaxRetries(7)
                        .startUpload(); //Starting the upload
                Toast.makeText(getApplicationContext(),
                        "Upload Successfull", Toast.LENGTH_LONG)
                        .show();

            } catch (Exception exc) {
                Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
            }

}

这是我的错误代码:

FATAL EXCEPTION: main
Process: com.indore.ddagro, PID: 6609
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
    at android.database.CursorWrapper.getString(CursorWrapper.java:137)
    at com.indore.ddagro.activity.RecepitActivity.getPath(RecepitActivity.java:230)
    at com.indore.ddagro.activity.RecepitActivity.uploadMultipart(RecepitActivity.java:153)
    at com.indore.ddagro.activity.RecepitActivity.onClick(RecepitActivity.java:276)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24697)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Force finishing activity com.indore.ddagro/.activity.RecepitActivity
Showing crash dialog for package com.indore.ddagro u0

2 个答案:

答案 0 :(得分:0)

错误的原因是您在数据库中的搜索在查询方法中返回0行。

第一种情况:表中可能没有任何内容,因为您在查询方法的所有参数中都传递了null

第二种情况:另一个查询也没有在查询方法中传递参数的特定表中找到任何内容。或者表格也是空的。

第三种情况:这可能是您内容提供商的内容,但我对此并不确定。

答案 1 :(得分:0)

您可能正在将filepath null传递给getPath(),因此请使用下面的if condition进行检查

 String path = "";

    if (!Uri.EMPTY.equals(filePath)) {

        path = getPath(filePath);
    }else{
        Toast.makeText(this, "Image not selected!", Toast.LENGTH_SHORT).show();
    }