简体   繁体   中英

Android : Get photo from gallery always returns bitmap not uri

I call:

Intent intent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);
        intent.putExtra("outputX", 256);
        intent.putExtra("outputY", 256);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("return-data", true);

startActivityForResult(intent, code);

then in my ActiviyResult():

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

                final Bundle extras = data.getExtras();
                if (extras != null) {
                //Get image
                Bitmap profilePicBitmap = extras.getParcelable("data");
                  ...

I try to get the uri by doing this:

data.getData()

but it always returns null

The problem is I need the path of the bitmap on the device so I can upload it elsewhere...but I can't get the path of it without having to convert it to uri. To convert to uri you have to request permission to write external storage, see here: ( http://stackoverflow.com/questions/12555420/how-to-get-a-uri-object-from-bitmap ) and I'd like to avoid that.

but it always returns null

That behavior will depend on the device and its apps.

I suspect that you will have better luck if you get rid of those undocumented extras. Presumably, rather than use an image-cropping library, you are trying to have the ACTION_PICK activity do the cropping. There are two problems with this:

  1. There is no requirement for the ACTION_PICK activity to do any cropping, as those extras are not part of the Android SDK.

  2. My guess is that since the cropped image does not exist as a file, they return it via the "data" extra (which is also outside the documented behavior of ACTION_PICK )

So, get rid of those extras. If you want to crop the image, use an image-cropping library .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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