15

I'm new to android. I want to pass bitmap into Bundle. But I can't find any solution for it. Actually, I'm confused. I want to display an image in a Dialog fragment. But I don't know how to put into Bundle. Should I send as PutByteArray()? But if I pass bitmap as an argument, it is stating as a wrong argument.

Here is my code:

public class MyAlert extends DialogFragment {
  Bitmap b;
  public MyAlert newInstance(Bitmap b) {
    this.b=b;
    MyAlert frag=new MyAlert();
    Bundle args=new Bundle();
    args.put("bitByte",b);
    frag.setArguments(args);
    return frag;
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    Bitmap bitmap=getArguments().getByteArray("bitByte");
    return new AlertDialog().Builder(getActivity());

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setView(R.id.fragid).create();
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Sayyaf
  • 316
  • 1
  • 4
  • 12
  • 9
    `Bitmap` is `Parcelable`, **but** passing the entire `Bitmap` is not a good solution, better pass the path or some other `Uri` pointing to your `Bitmap` – pskink Nov 19 '15 at 06:49
  • 2
    http://stackoverflow.com/questions/12908048/passing-bitmap-between-two-activities Use the logic to save it in the bundle. – thepace Nov 19 '15 at 07:03
  • Possible duplicate of [Passing android Bitmap Data within activity using Intent in Android](https://stackoverflow.com/questions/11010386/passing-android-bitmap-data-within-activity-using-intent-in-android) – AdamHurwitz Jun 17 '19 at 05:12

5 Answers5

39

No need to convert bitmap to byte array. You can directly put bitmap into bundle. Refer following code to put bitmap into bundle.

bundle.putParcelable("BitmapImage",bitmapname);

Get bitmap from Bundle by following code

Bitmap bitmapimage = getIntent().getExtras().getParcelable("BitmapImage");
Shashi Ranjan
  • 621
  • 1
  • 4
  • 6
  • This is probably the way to go if your bundle is use for IPC. If you attempt to store it you will likely get the following error: 'Tried to marshall a Parcel that contained Binder objects'. In which case just use the above byte array solution https://stackoverflow.com/a/33797090/3969362 – Slion Apr 13 '20 at 21:20
18

First of all convert it to a Byte array before adding it to intent, send it out, and decode.

//Convertion to byte array

  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
  byte[] byteArray = stream.toByteArray();

Bundle b = new Bundle();
b.putByteArray("image",byteArray);


  // your fragment code 
fragment.setArguments(b);

get Value via intent

byte[] byteArray = getArgument().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Amarjit
  • 4,327
  • 2
  • 34
  • 51
4

If you want to pass image using bundle then i am sure it will help you.

Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
fragment.setArguments(bundle);
1

if you are using NavigationComponent, you should use safeArgs !

you can put arguments in nav_graph like this :

<argument
        android:name="profileImage"
        app:nullable="true"
        app:argType="android.graphics.Bitmap" />

and send it like it : (First Fragment)

findNavController().navigate(SettingFragmentDirections.actionSettingFragmentToHomeFragment(bitmap))

and give it like this : (Second Fragment)

 val bitmapimage =
            arguments?.getParcelable<Bitmap>("profileImage")


        user_profile_img.setImageBitmap(bitmapimage)

read more : https://developer.android.com/guide/navigation/navigation-pass-data

Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
0

I think it is easier to send the path or address of the image as a string and load it on the other side.

If the image is a web address, you can use Glide or Picasso libraries and cache them, so on the other activities or fragments it will not load twice.

Ashkan Ghodrat
  • 3,162
  • 2
  • 32
  • 36
  • But I'm not allowed to use third parties libraries. and pls send sample code of image as string. Actually i have used the uri and displayed the image on the activity. when i click on that image, that image should appear in Dialog fragment. – Sayyaf Nov 19 '15 at 07:16
  • I didnt say the image as string, i meant the path, and if you have shown the image, i think you are done, but i can update the answer if you want. – Ashkan Ghodrat Nov 19 '15 at 07:18
  • the issue right now im facing is how to use that bitmap and send to the dialog fragment and display on it. I'm unable to understand the code on Andriod Developers site. kindly if can help, appreciated. – Sayyaf Nov 19 '15 at 07:21
  • from now on, please post your questions with more details. for example you should have said how to show an image on a dialog. – Ashkan Ghodrat Nov 19 '15 at 07:25
  • no need to apologies, most of the questions you have (considering you are new to android,) have already answered on this site, so you can look for them before posting a new one, it will save you some time. – Ashkan Ghodrat Nov 19 '15 at 07:33