无法使用ACTION_PICK意图选择特定图像

时间:2013-03-08 00:29:48

标签: android android-intent

我正在使用这样的意图:

Intent intent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

onActivityResult()我有这个:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) {
        return; // user cancelled
    }

    Uri imageUri = data.getData();
    if (imageUri == null) {
        // (code to show error message goes here)
    return;
    }

    // Get image path from media store
    String[] filePathColumn = { android.provider.MediaStore.MediaColumns.DATA };
    Cursor cursor = this.getContentResolver().query(imageUri, filePathColumn,
            null, null, null);

    if (cursor == null || !cursor.moveToFirst()) {
        // (code to show error message goes here)
        return;
    }

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

    if (imagePath == null) {
        // error happens here
    }
}

当我从特定相册中选择图像时,例如“帖子”“个人资料照片”(见屏幕截图)我无法获取{{1}中的图像路径}}。可以毫无问题地选择其他相册中的图像。

Galley screenshot

我已尝试添加onActivityResult(),但intent.putExtra("return-data", true)会在data.getExtras()中返回null

similar question here,但没有人接听。

请帮忙!

2 个答案:

答案 0 :(得分:6)

跳跃这会对你有帮助....

ACTIVITYRESULT_CHOOSEPICTURE是调用startActivity时使用的int(intent,requestCode);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    final InputStream ist = ontext.getContentResolver().openInputStream(intent.getData());
    final Bitmap bitmap = BitmapFactory.decodeStream(ist, null, options);
    ist.close();
  }
}

如果上面的代码不起作用,只需参考此链接......它将显示方式

http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/

答案 1 :(得分:1)

试试这个:

String selectedImagePath = imageUri.getEncodedPath();

它适用于我使用图库图像选择器

也许这个:

bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());