从库中选择图像后,Android应用程序崩溃 - cursor.getColumnIndex返回-1

时间:2015-10-12 02:00:43

标签: java android google-apps

我正在尝试获取图库或相机中图像的绝对路径。

相机意图打开后,我点击图像&我试图从URI获取文件路径。请参阅代码位。

//In the OnActivityResult:

Uri uri = data.getData();
File myFile = new File(uri.getPath());
File imageFilePath = new File(getRealPathFromURI(uri));

//The method to fetch path:

private String getRealPathFromURI(Uri contentURI) {
    String result;
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    Log.d("", "cursor = " +cursor);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        result = contentURI.getPath();

    } else {
        int idx = cursor.getColumnIndex(MediaStore.Images.Media.DATA ); // Is this the wrong way to get the image cursor?
        Log.d("", "came here = " +idx); // Here idx us -1!!!
        cursor.moveToFirst();
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

// image pick intent

public void imageFromGallery() {
    Intent intent = new Intent();
// Show only images, no videos or anything else
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);


}

添加logCat:

 721-4721/com.coolhydro.copy E/CursorWindow﹕ Failed to read row 0, column -1    from a CursorWindow which has 1 rows, 6 columns.
10-11 18:53:55.495    4721-4721/com.coolhydro.copy D/AndroidRuntime﹕ Shutting down VM
10-11 18:53:55.495    4721-4721/com.coolhydro.copy E/AndroidRuntime﹕ FATAL  EXCEPTION: main
    Process: com.coolhydro.copy, PID: 4721    
    java.lang.RuntimeException: Failure delivering result    ResultInfo{who=null, request=1, result=-1, data=Intent {     dat=content://com.android.providers.media.documents/document/image:49 flg=0x1 }}    to activity {com.coolhydro.copy/com.coolhydro.copy.MainActivity}:    java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.      Make sure the Cursor is initialized correctly before accessing data from it.
            at    android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
            at     android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
            at android.app.ActivityThread.-wrap16(ActivityThread.java)   
        at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
            at android.os.Handler.dispatchMessage(Handler.java:102)  
            at android.os.Looper.loop(Looper.java:148) 
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1   from CursorWindow.  Make sure the Cursor is initialized correctly before    accessing data from it.
            at android.database.CursorWindow.nativeGetString(Native Method)
            at  android.database.CursorWindow.getString(CursorWindow.java:438)
            at   android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51    )
            at  android.database.CursorWrapper.getString(CursorWrapper.java:137)
            at com.coolhydro.copy.MainActivity.getRealPathFromURI(MainActivity.java:80)
            at com.coolhydro.copy.MainActivity.onActivityResult(MainActivity.java:52)
            at android.app.Activity.dispatchActivityResult(Activity.java:6428)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
            at android.app.ActivityThread.-wrap16(ActivityThread.java)
            at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-11 18:54:01.237    4721-4730/com.coolhydro.copy W/CursorWrapperInner﹕   Cursor finalized without prior close()
10-11 18:58:55.523    4721-4721/? I/Process﹕ Sending signal. PID: 4721 SIG: 9

谢谢!

2 个答案:

答案 0 :(得分:1)

您需要将投影传递给查询调用。按原样,返回没有列的游标,因此当您尝试查找某列的索引时,返回-1。尝试使用

String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentURI, projection, null, null, null);

然后,当您想要检索数据时,您已经知道列索引(它是投影数组中该列的索引),因此您只需设置

int idx = 0;

答案 1 :(得分:1)

当我从画廊和相机请求图片时,我有这个代码:

     Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        pickIntent.setType("image/*");



        Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileCameraCapturedImage));

        String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
        Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
        chooserIntent.putExtra
                (
                        Intent.EXTRA_INITIAL_INTENTS,
                        new Intent[]{captureIntent}
                );


        startActivityForResult(chooserIntent, REQUEST_CODE_ADD_PHOTO);

在我的onActivityResult()我检查返回的数据来自画廊或相机,然后我使用它:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK){
            if(requestCode == REQUEST_CODE_ADD_PHOTO) {
                Log.v(TAG, "Photo ok! " + data);
                boolean isCamera;


                //getclip data requires API 16
                if (Build.VERSION.SDK_INT >= 16){

                    if (data == null || (data.getData() == null && data.getClipData() == null)) {
                        isCamera = true;
                    } else {
                        final String action = data.getAction();
                        if (action == null) {
                            isCamera = false;
                        } else {
                            isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        }
                    }
                }
                else{
                    if (data == null || data.getData() == null) {
                        isCamera = true;
                    } else {
                        final String action = data.getAction();
                        if (action == null) {
                            isCamera = false;
                        } else {
                            isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        }
                    }
                }

                Log.v(TAG, "is camera: " + isCamera);
                if(!isCamera) {
                    //gallery result

                    Uri selectedImageUri = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getContentResolver().query(selectedImageUri, filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String filePath = cursor.getString(columnIndex);
                    cursor.close();


                    Log.v(TAG,"Path: "+filePath);
                    if(filePath !=null) {
                        if(!filePath.equalsIgnoreCase("")) {
                            imagePath = filePath;
                            Picasso.with(getApplicationContext()).load("file://" + imagePath).into(ivPhotoContainer);
                            btnAddChange.setText("Change");
                        }
                    }
                }
                else {
                    //is camera result
                    Log.v(TAG, "is camera result");

                    String path = fileCameraCapturedImage.getAbsolutePath();

                    imagePath = path;
//                    ivPhotoContainer.setImageBitmap(thumbnail);
                    Picasso.with(getApplicationContext()).load("file://"+imagePath).into(ivPhotoContainer);
                    btnAddChange.setText("Change");




                }
            }
        }
    }

注意:fileCameraCapturedImage的类型为file,它引用捕获的图像将保存在设备上的文件。 私人文件fileCameraCapturedImage = new File(Environment.getExternalStorageDirectory()+File.separator+"capturedImage.jpg");