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);
Related
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);
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.
Can someone help me to solve a problem in Android Studio? I am creating an app in which I have a photo in JPEG format(Comming from the camera, not from file) and I want to convert it to bitmap.
You can use this method to convert image to byte[]. Like this:
public byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
// Here image variable is a JPEG file/image.
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
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){
}
I'm using the Parse SDK to build my app.
I can easily get a Bitmap out of the gallery by using Intents. I want to use them as a profile picture for my users.
However, in order to upload it I must convert it into a byte array. Also, when I download it it comes in the form of a byte array, and I must convert it back to a Drawable.
In order to convert it into a byte array, I'm doing this:
public static byte[] bitmapToByteArray(Bitmap bmp)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
I cannot find how to convert this byte[] back into a Bitmap without having to save it first. How can this process be achieved?
Just do the following:
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);