How to pass bitmap from one activity to another

后端 未结 4 1656
再見小時候
再見小時候 2020-12-04 02:11

i have bitmap in ActivityA i want to pass the bitmap from here to ActivityB, i googled for this. when i use this

Intent intent = new Intent(this, NewActivit         


        
相关标签:
4条回答
  • 2020-12-04 02:43

    I tried and its working as below using intent.putExtra("name", bitmap)

    While passing with intent,

    Intent intent = new Intent(Current.this, Next.class);
    intent.putExtra("bmp", bitmap);
    startActivity(intent);
    

    While fetching,

    Bitmap bitmap = getIntent().getParcelableExtra("bmp");
    

    OR

    Other option is to use Application class,

    You can also use a class that extends Application and have a setter getter for Bitmap and call it from every Acitivity.

    ((myApplication_class_name)getApplication()).setBitmap(bmp);
    

    and fetch the Bitmap using,

    ((myApplication_class_name)getApplication()).getBitmap();
    
    0 讨论(0)
  • 2020-12-04 02:43

    I dont think that is the right method... you can use this link for the feature to be implemented. i too have used something like this itself.

    0 讨论(0)
  • 2020-12-04 02:54

    You can simply name you Bitmap as static first.

    then create a method like

    public static Bitmap getBitmap(){
    return bitmap;
    }
    

    then you can simply call from other activities,

    bitmapwantedclass.getBitmap();
    

    Hope it helps

    0 讨论(0)
  • 2020-12-04 02:58

    Your code is correct for putting bitmaps into the extras and works fine for me with small images. But it seems that there is a limit for the size of the Parcelable extra. See http://groups.google.com/group/android-developers/browse_thread/thread/7322a84adcfee567?pli=1.

    You might want to store the image first and only hand over the URI to the store location.

    Edit: Using a public static field for the bitmap as suggested by udaykiran violates so many OO principles I don't even know where to start.

    0 讨论(0)
提交回复
热议问题