How to Passing Image (bitmap) to another activity.

Selasa, April 10, 2018
So on my journey of learning,
I try to pass image from one activity to another activity,
which is the image that we want to pass is bitmap value

 final String ship_thumbnail = shipDataByPosition.getFotoKapal();  
     byte[] decodedString = Base64.decode(ship_thumbnail, Base64.DEFAULT);  
     final Bitmap decodedShipImage = BitmapFactory.decodeByteArray(decodedString, 0 , decodedString.length);  
     final ByteArrayOutputStream decodedShipToByteArray = new ByteArrayOutputStream();  
     decodedShipImage.compress(Bitmap.CompressFormat.JPEG,100, decodedShipToByteArray);  

ship_thumbnail is variable that save blob string from the database, I use JSON parser.
then it is decoded back and saved as variable decodedShipImage

so, decodedShipImage is the bitmap value that I want to pass to another activity.

change decodedShipImage to ByteArray type value first, see the variable decodedShipToByteArray
because the Bundle on Android to passing value to other activity have limited memory size, specially on bitmap if our image have big size.

 holder.cardView.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) { 

bundle.putByteArray("image_ship", decodedImageToDetail.toByteArray());  

  Intent i = new Intent(mContext.getApplicationContext(), DetailShip_Activity.class);  
         i.putExtras(bundle);  
         mContext.startActivity(i);  
       }  
     });  


then on other activity, get the value of  "image_ship" from the Bundle

 bundle = getIntent().getExtras();  
 byte[] ship_image = bundle.getByteArray("image_ship");  
 Bitmap showImage = BitmapFactory.decodeByteArray(ship_image, 0, ship_image.length);  
 ivShipsPhoto = (ImageView) findViewById(R.id.ivShipImage);  
 ivShipsPhoto.setImageBitmap(showImage);  

based on searching on sites and StackOverflow, and already used.

caution: if the Image Size is so big, Java Binder Failure Exception will be triggered.
you can use LruBitmapCache/LruCache (will post it later)

Tidak ada komentar:

Diberdayakan oleh Blogger.