Unable to select particular images using ACTION_PI

2019-01-14 19:59发布

问题:

I'm using an intent like this:

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

And in onActivityResult() I have this:

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

When I select images from particular albums like "Posts", "Profile Photos" (see screenshot) I'm unable to get the image path in onActivityResult(). Images from other albums can be selected with no problems.

I've tried adding intent.putExtra("return-data", true) but data.getExtras() returns null in onActivityResult().

There is similar question here, but no one answered it.

Please help!

回答1:

hops this will helps you ....

ACTIVITYRESULT_CHOOSEPICTURE is the int you use when calling startActivity(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();
  }
}

if above code doesn't work than just refer this link... it will surly shows the way

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



回答2:

try this:

String selectedImagePath = imageUri.getEncodedPath();

it works for me using gallery image picker

maybe this:

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