0

我在这里这里看到了类似的问题。

但是这些仍然没有解决我的问题,所以我发布了一个新问题。

这是我的代码来获取在照片应用程序中选择的图像的路径。但我没有得到 cursor.getColumnIndexOrThrow(column); 值为0和 cursor.getString(column_index) 值为null

Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }

谁能帮我解决这个问题。如果我的问题太宽泛,请告诉我。我会更新我的问题。

更新: 起始意图:

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, getString(R.string.selectPhoto)), RESULT_LOAD_IMAGE);

在 onActivityResult 中:

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(contentURI, projection, null, null, null);
    if (cursor == null) {
          return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(projection[0]);
        return cursor.getString(idx);
    }
}

问候

4

1 回答 1

0

用户下面的代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Common.Action_Gallery:
                    if (data != null) {
                        Uri selectedImage = data.getData();
                        String[] filePathColumn = {MediaStore.Images.Media.DATA};
                        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                        cursor.moveToFirst();
                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                        String picturePath = cursor.getString(columnIndex);
                        cursor.close();                           
                    }
                    break;   
            }
        }
}
于 2016-08-02T07:10:15.240 回答