2

I have an a bitmap image in my Activity like this:

Bitmap image4 = BitmapFactory.decodeResource(getResources(), R.drawable.hello);
        image1.setImageBitmap(image4);

I'm trying to pass this image in fragment using this code:

  FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.addToBackStack(null);
                Secondfrag yourFragment = new Secondfrag();
                Bundle b = new Bundle();
                b.putParcelable("BitmapImage",image4);

                yourFragment.setArguments(b);
                fragmentTransaction.add(R.id.frag, yourFragment, "FRAGMENT");
               fragmentTransaction.commit();

and trying to get this image in fragment like this:

Bundle b=this.getArguments();
        String page=b.getString("BitmapImage");

        image2.setImageURI(Uri.parse(page));

But there is no image display in my Fragment.How to resolve this issue?

anshu
  • 23
  • 1
  • 4

5 Answers5

2

Convert it into 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 from destination fragment

byte[] byteArray = getArgument().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

Credit to https://stackoverflow.com/a/33797090/4316327

Community
  • 1
  • 1
Harry T.
  • 3,478
  • 2
  • 21
  • 41
0

Passing image reference is a good way rather than passing whole image data.

In your case you could pass your image Uri

If you want to get image from internal storage the you can pass Uri. But if you just want a specific resource (i.e. drawable) then, it could just pass its name (which is String).

You can check this solution Passing Uri to an Intent

Community
  • 1
  • 1
Tahmid Rahman
  • 748
  • 1
  • 8
  • 20
0

You can just pass your image path/URI in the bundle.

In your case you are using your resources to make the Bitmap. You can use the same code to get the bitmap in your fragment. Resources are available in the fragment as getActivity().getResources();

Bitmap image4 = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.hello);
    image1.setImageBitmap(image4);
0

Please try this method

private void nextpageintent(String photoUri){

Bundle bundle = new Bundle();
bundle.putString("photo", photoUri);
picture picture = new picture();
picture .setArguments(bundle); 
FragmentTransaction transaction = null;
transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, picture); 
transaction.addToBackStack(null);
transaction.commit();

}

Prabha Karan
  • 1,309
  • 3
  • 17
  • 35
0

Here, this issue you can simply solve using getter setter of the Singleton class, First make one Ben class for your bitmap storage.

class BitmapStorage {
private static volatile BitmapStorage instance = null;
private Bitmap bitmap = null;
private BitmapStorage () {}

    public static BitmapStorage getInstance() {
        if (instance == null) {
            synchronized(BitmapStorage.class) {
                if (instance == null) {
                    instance = new BitmapStorage();
                }
            }
        }
        return instance;
    }

    public Bitmap getBitmap(){
           return bitmap;
    }

    public void setBitmap(Bitmap bitmap){
           this.bitmap = bitmap;
    }
}

Then after in your activity set bitmap into your model class. Like

BitmapStrorage bs = getInstance ();
Bitmap image4 = BitmapFactory.decodeResource(getResources(), R.drawable.hello);
Bs.setBitmap(image4);

whenever fragment call according to fragment life cycle then OnCreate method first call, so here we can check bitmap is null or not and get bitmap and use it. may not check this solution but i thing it's work for you.

Kishan Donga
  • 2,851
  • 2
  • 23
  • 35