【问题标题】:Not getting the image from android photos app没有从android照片应用程序获取图像
【发布时间】:2016-08-02 07:03:53
【问题描述】:

我在 SO herehere 中看到了类似的问题。

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

这是我获取在 照片 应用中选择的图像路径的代码。但我没有得到 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);
    }
}

问候

【问题讨论】:

    标签: android android-gallery android-image-capture


    【解决方案1】:

    代码下方的用户

    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;   
                }
            }
    }
    

    【讨论】:

    • 在 cursor.getString(columnIndex) 处仍然为空;打电话
    • 我的 data.getData 给出的响应为“content://com.google.android.apps.photos.contentprovider/0/1/mediaKey%3A%2FAF1QipP_19ASFn4CF_8XmzBwXI4WRR9SSDTgR7ZopL2v/ACTUAL”
    • 我相信您并没有对代码进行太多更改,因为这段代码在我的应用程序中运行并在各种设备上进行了测试。
    • 你能测试一下,你能从默认照片应用程序中获取图像吗?从顶部的照片选项中选择一张图片,请告诉我。?
    • 我已经测试过了。我能够从默认照片应用程序中获取图像。您能否分享一下您用来调用意图并获得结果的新代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多