简体   繁体   中英

How i pass Bitmap or Drawable from one Activity to another?

I would like to pass Bitmap or Drawable from one Activity to another, now i'm doing this:

            EventPhoto photo = (EventPhoto) parent.getItemAtPosition(position);

            BitmapDrawable test = (BitmapDrawable) photo.getDrawable();
            Bitmap bitmap = test.getBitmap();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos); 
            byte[] b = baos.toByteArray();

            Intent intent = new Intent(EventDetailActivity.this, ImageExpandActivity.class);
            System.out.println(b.length); //B LENGTH = 908k
            intent.putExtra("image",b);
            startActivity(intent);

My problem is when i try to use putExtra, he don't send any exception, nothing, but don't respond and Android kill application, why this is happen? I'm thinking its about size because everything works when i pass a little image.

I'm looking for and find few solutions like this: How to pass bitmap from one activity to another but i want to use putExtra, any ideas?

Thanks

I won't go to disc (files, db, etc) if you just need this data temporarily. I'd go for your same approach.

You have several options for that. Just take whatever fits better to your app.

  • public static class, available all along your code. This kind of helpers are handy for managing things like shared preferences or some global information you might need to have there

  • Singleton , this pattern is very similar to the previous one. Main difference is that in this case you have just one instance of the class (in static there's no instance, just access to vars and methods). This pattern is helpful whenever you need any reference instance for the whole lifecycle of your app.

  • Extend application. This goes a step further from the other two. You have an instance of your Application available in the whole lifecycle. Normally you may want to manage some other stuff there, like push, or error notifications, services, etc. Example

Well, I can't find any different solution so I solved with this: How to pass bitmap from one activity to another

same link I gave before with static attribute:

public static Bitmap getBitmap(){
   return bitmap;
}

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