transferring bitmap between activities - android

bmp1 is a bitmap image
in activity 1 I have the following code
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp1.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
regIntent.putExtra("chosenImage",byteArray);
then in activity2 i do this.
Intent regIntent = getIntent();
byte[] byteArray = regIntent.getByteArrayExtra("chosenImage");
bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
bmp = Bitmap.createScaledBitmap(bmp, 150, 200, true);
i then display the image using this code:
ImageView mainBookImage = findViewById(R.id.mainBookImage); //uncoment to load image
mainBookImage.setImageBitmap(bmp);
however i just end up getting a blank image, any ideas why?

In your First Activity.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp1.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MainActivity.class);
intent.putExtra("chosenImage", byteArray);
startActivity(n);
In your Second Activity oncreate()
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("chosenImage");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
// And Set Image in Imageview like this
ImageView mainBookImage = findViewById(R.id.mainBookImage);
if (bmp != null) {
mainBookImage.setImageBitmap(bmp);
}

I would recommend you to implement different approach.
If its mandatory for you to pass the bitmap itself, then you can pass the Bitmap object as it is since it implements Parcelable. It would be as simple as regIntent.putExtra("chosenImage",bmp1); and in activity 2:
final Bitmap bmp1= regIntent.getParcelableExtra("chosenImage");
However, this approach is highly discouraged since it will cost a lot of memory and impact responsiveness of your application. Moreover on low end devices this could lead to crash due to lack of memory and also limitation on data size which could be transferred through Intents.
One approach could be to store it in a file and then pass the path of file through Intent extra. In Activity2 you can retrieve the path -> load the image in Bitmap -> use in ImageView.

Related

pass imagview resource to intent

How can I pass an ImageView resource from one activity to another activity?
Have tried imagview.resource and imageview.drawable to pass this data through intent. But neither works.
![enter image description here][1]
Below, I want to send image 1 to add in recyclerview of image 2.
![enter image description here][2]
unreachable, but you can get the value you want by making imageview.setTag (key, tag) and then pulling it with the imageview.getTag() function.
Convert drawable to Bitmap and then into byte array form and then you can pass it via intent to another activity
See Below link :
Passing image from one activity another activity
Convert Bitmap to Byte Array:-
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Pass byte array into intent:-
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
Get Byte Array from Bundle and Convert into Bitmap Image:-
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);

how to transfer a bitmap to another activity?

When I capture using camera2 api,image is made and transfer the image to bytes next to bitmap. My purpose is to select save or not after capturing.
So It will be not made in file before Pressing save btn.
below : send side
Bitmap bitmap = textureView.getBitmap();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,bs);
byte[] byteArray = bs.toByteArray();
below : receieve side
byte[] byteArray = getIntent().getByteArrayExtra("byteArray");
bitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
resultView.setImageBitmap(bitmap);
and I got received the error like below
android.os.TransactionTooLargeException
I understand the cause of error But I wanna transfer the image to another activity
Is there anyone who help this?
Put your bitmap object in Intent.putExtra("key", object),
intent.putExtra("btimap", bitmap);
Get it using Intent.getParcelableExtra("key"),
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("btimap");
Convert it to a Byte array before you add it to the intent, send it out, and decode.
//Convert to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);
Then in Activity 2:
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

OutOfMemoryError getting while converting an image to circular shaped one [duplicate]

I am using a bitmap. It throws out of memory error (2 out of 5 times).
How can it be avoided.
Following is my code:
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
photo_new= rotateImage(bitmap, 90);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo_new.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent i = new Intent(getApplicationContext(),new_class.class);
i.putExtra("image", byteArray);
startActivity(i);
byteArray=null;
You are getting OutOfMemoryError because you haven't recycle
those bitmaps you used
try to recycle those bitmaps after you used them
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
photo_new= rotateImage(bitmap, 90);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo_new.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
bitmap.recycle();
Intent i = new Intent(getApplicationContext(),new_class.class);
i.putExtra("image", byteArray);
startActivity(i);
byteArray=null;
byte[] byteArray = stream.toByteArray();
in that line your ram is getting filled by whole Bitmap. Change bitmap quality from 100 to 50-60 as below
photo_new.compress(Bitmap.CompressFormat.JPEG, 50, stream);
or
photo_new.compress(Bitmap.CompressFormat.JPEG, 60, stream);
try both and see the results.
wrong
1). Bitmap quality is high.
2). you are not using try catch.
Suggestions
1). reduce the quality of bitmap to 45-50.
2). use try catch block to prevent your app from crash.
Solution // sender activity
try{
Intent _intent = new Intent(this, newscreen.class);
Bitmap _bitmap; // your bitmap
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
_bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
i.putExtra("byteArray", _bs.toByteArray());
startActivity(i);
}catch(Exception e){
}
receiver activity
try{
if(getIntent().hasExtra("byteArray")) {
ImageView _imv= new ImageView(this);
Bitmap _bitmap = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
_imv.setImageBitmap(_bitmap);
}
}catch(Exception e){
}

compress image before pass to another activity in android

want i want to do is take a picture with my camera and than pass that image to another activity, in the next activity i would like to get that image and upload it. i tried using the following code and it works but the quality is very low.
private void putBitmapToIntentAndStartActivity(Bitmap bitmap)
{
Intent i = new Intent(getActivity(), ImagePreviewActivity.class);
// Bitmap b=scaleDown(bitmap,400,true);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bs);
i.putExtra("imageByteArray", bs.toByteArray());
startActivity(i);
}
if i remove the marked line with the scale downmethod it wont work. if i use it it works but the quality is very low.
is there any ideas how to pass that image and get it in the next activity with its full quality?
thanks! im sure its a very common issue.
If you want to tranfer bitmap between Activites/Fragments
Activity
To pass a bitmap between Activites
Intent intent = new Intent(this, Activity.class);
intent.putExtra("bitmap", bitmap);
And in the Activity class
Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
Fragment
To pass a bitmap between Fragments
SecondFragment fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
fragment.setArguments(bundle);
To receive inside the SecondFragment
Bitmap bitmap = getArguments().getParcelable("bitmap");
Transfering Large Bitmaps (Compressing)
If you are getting failed binder transaction, this means you are exceeding the binder transaction buffer by transferring large element from one activity to another activity.
So in that case you have to compress the bitmap as an byte's array and then uncompress it in another activity, like this
In the FirstActivity
Intent intent = new Intent(this, SecondActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
byte[] bytes = stream.toByteArray();
intent.putExtra("bitmapbytes",bytes);
And in the SecondActivity
byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

I want to Passing a image from Drawable from one activity to Another via Intent?

I am using this part of code to send the image...
public void onClick(View v) {
Intent goto_membersdealsdetails = new Intent(
"com.abc.cd.FE");
goto_membersdealsdetails.putExtra("valueq", R.drawable.app_icon);
v.getContext().startActivity(goto_membersdealsdetails);
And to get the image i am using this sort of code...
imag_link = getIntent().getStringExtra("valueq");
Toast.makeText(getApplicationContext(), imag_link, Toast.LENGTH_LONG)
.show();
Its Toast is providing a blank toast....
I want to set the image to a certain imageview....Suggestion please
you can pass byte array and get bytearray and then make bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);
In Activity B you receive intent with byte array (decoded picture) and apply it as source to ImageView:
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
You are passing an int value in intent. so call
imag_link = getIntent().getIntExtra("valueq", value);

Categories

Resources