繁体   English   中英

无法从android照片应用获取图像

[英]Not getting the image from android photos app

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

但是这些仍然不能解决我的问题,所以我要发布一个新问题。

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

问候

用户下面的代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM