compress image before pass to another activity in android - 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);

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);

transferring bitmap between activities

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.

How to pass an image between Activities in Android?

I want to click a button from Activity 1 with image to show an image for Activity 2, but it's not working.
Activity1:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
// Bundle bundle = new Bundle();
Drawable drawable=img1.getDrawable();
Bitmap bitmap= ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
intent.putExtra("picture", b);
// String ten = edt.getText().toString();
// bundle.putString("tenkh", ten);
// intent.putExtras(bundle);
startActivity(intent);
Activity2:
ImageView image = (ImageView) findViewById(R.id.img2);
TextView txtTen= (TextView) findViewById(R.id.tv1);
Intent goiIntent=getIntent();
Bundle extras = goiIntent.getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
image.setImageBitmap(bmp);
You should show us the logical to help us understand the problem.
However i think your problem is that the byte [] is to big to be pass in an intent.
One workaround could be to have an abstract class with a public static field of type byte[]. Before broadcasting your intent, set this field with your data and in the next activity read this data and don't forget to set the field to null when you don't need it to avoid memory leak.
Create a class ImageHelper as follow :
public abstract class ImageHelper {
public static byte [] image;
}
In your activity 1, before launching the intent, instead of intent.putExtra("picture", b); use ImageHelper.image = b;
Then in your activity 2, instead of byte[] b = extras.getByteArray("picture");, you use byte[] b = ImageHelper.image;.
Activity 1
intent.putExtra("BitmapImage", bitmap);
Activity 2
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
ImageView image = (ImageView) findViewById(R.id.img2);
image.setImageBitmap(bitmap);

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