简体   繁体   中英

MediaStore.Images.Media.getBitmap fails unexpectedly

I'm building an android app that can get images from the photos gallery.

two questions if I may:

1)

 Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String action = intent.getAction();

        Uri uri = null;

        // if this is from the share menu
        if (Intent.ACTION_SEND.equals(action)) {
            if (extras.containsKey(Intent.EXTRA_STREAM))
            {
                try
                {
                    // Get resource path from intent callee
                     uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);

I have this uri:

content://media/external/images/media/27031

but my code fails here with no information (it just crashes), no log cat, not console error.

 try {
            if (uri !=null)
            {

            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

            imageView.setImageBitmap(bitmap);
            }

        } catch (IOException e) {
            e.printStackTrace();

            Log.e(this.getClass().getName(), e.toString());
        }

how can I solve this?

(it works for this url: Uri uri = Uri.parse("content://media/external/images/media/22719");

2) i plan to save all uri's in a data structure. That's the recommended way?

or is it better to keep the photos in a local app folder?

Update

I think the problem was that the failing image was on the external SD whereas the succeeding one was on a local SD.

How can i enable getting image from the external SD ?

some code for getting a bitmap from a 'share' action on a photo...

    Object obj = null;

        if (Intent.ACTION_SEND.equals(_action) && _type != null) {
            obj = _bundle.get("android.intent.extra.STREAM");
            if( getIntent().getType().startsWith("image/") && obj != null){
              if (obj instanceof android.net.Uri){ get((Uri)obj);}}

  get(Uri uri){
      Bitmap _bit = BitmapFactory.decodeStream(
       getContentResolver().openInputStream(uri),null,options);
}

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