I sent the picture from the camera to the other activity. I'm trying to do it with an intent. But I got an error. How can I resolve FAILED BINDER TRANSACTION error.
Intent intent = new Intent(this, B.class);
byte[] byteBitmap = converttoByte(mNextPageBitmap);
intent.putExtra("bitmap", byteBitmap);
startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
B.class
if(getIntent().hasExtra("bitmap")) {
byte[] getByte = getIntent().getByteArrayExtra("bitmap");
mBitmap = BitmapFactory.decodeByteArray(getbyte, 0, getbyte.length);
bitmapDrawable = new BitmapDrawable(getResources(), mBitmap); }
Putting a bitmap in a Intent is a bad idea. There's a maximum byte size on an Intent. Write it to a file, and pass the filename in the intent.
Related
I am trying to send an image that in ListView from onItemClick to onCreate method in another activity .
This is my code :
Activity 1
product_imageView= findViewById(R.id.product_imageView);
byte[] image = savedInstanceState.getByteArray("Product image");
Bitmap receivedImage = BitmapFactory.decodeByteArray(image,0,image.length);
//here it give me error
product_imageView.setImageResource(receivedImage);
Activity 1
String productimage = productslist.get(position).getProductImage();
Bundle bundle = new Bundle();
bundle.putByteArray("Product image", productimage.getBytes());
intent.putExtras(bundle);
startActivity(intent);
Please try this
First convert imageview to bitmap.
product_imageView.buildDrawingCache();
Bitmap bitmap = product_imageView.getDrawingCache();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("BitmapImage", bitmap);
In second Activity please include this line:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
imageview.setImageBitmap(bitmap);
Why are you sending images between activities?, why not just place your image in drawable folder and pass the name or path of the image to the next activity and then display or do whatever you want to do with it.
You do it like this
put your image in drawable folder for example image1.png.
then all you do is parse a string with the name for the image to the second activity example:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("image", "image1.png");
startActivity(intent);
On the second activity you display for instance:
Intent intent = getIntent();
String imageStr = intent.getStringExtra("image");
ImageView imageView = (ImageView)findViewById(R.id.myimage);
imageView.setImageResource(R.drawable.image1);
I hope code is correct, forgive my mistakes I wrote this in a hurry.
I am not able to pass image to another activity as bitmap
Main activity
Intent intent=new Intent(LastActivityGrid.this,ActivityMetadata.class);
byte[] image = Utils.getImageBytes(gridlist.get(position).getBitmap());
intent.putExtra("image",image);
startActivity(intent);
second activity
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
Bitmap implements Parcelable, so you could always pass it with the intent:
Intent intent=new Intent(LastActivityGrid.this,ActivityMetadata.class);
Bitmap image = gridlist.get(position).getBitmap();
intent.putExtra("image",image);
startActivity(intent);
and retrieve it on the other end:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("image");
If Image is not too bit then It will work.
But proper solution is to pass the actual location of the image and get the Image from there in the second activity. But As I understand you prefer quick solution.
I hope it will help!
Here's the Code to pass and get image through intent,
secondActivity
Intent intent = new Intent(this, PostVideoTagLocation.class);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.index);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent2 = new Intent();
intent2.putExtra("picture", byteArray);
startActivityForResult(intent,1);
setResult(RESULT_OK, intent2);
firstActivity
if (requestCode == 1) {
if(resultCode == RESULT_OK){
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.add_photo);
image.setImageBitmap(bmp);
}
}
Inside PostVideoTagLocation's onCreate method you can simply create a bitmap from the byte array that the creating activity has passed.
getIntent() method results in the intent that was used to create this activity, which will contain a bundle containing the picture bytearray.
However I am not completely sure how safe is it to store possibly big images inside an Bundle passed with Intent, so I would use this with caution.
You can use Intents getByteArrayExtra() with parameter "picture" which is used when you're setting the extra, and then simply with BitmapFactorys decodeByteArray decode it to an Bitmap.
Your code might looklike somethign like this
onCreate(Bundle bundle)..
byte[] rawBitmap = getIntent().getByteArrayExtra("picture");
Bitmap bitmap = BitmapFactory.decodeByteArray(rawBitmap)
and do with the bitmap whatever you want.
However I myself would save the image to a temporarily file and pass it with intent, just to be safe.
On your first Activiy you should call the second Activiy like this:
Intent intent = new Intent(PostVideoTagLocation.this, SecondActivity.class);
startActivityForResult(intent, 1111);
Then on Second Activity when you want to send image to first Activiy you should call:
Intent intent = new Intent();
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.index);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("picture", byteArray);
setResult(RESULT_OK, intent);
finish();
Finally in the first Activity write this code on onActivityResult:
if(requestCode == 1111){
Bundle extras = data.getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
mImageView.setImageBitmap(bmp);
}
It should work (I tested it). Hope it helps you!
[EDIT] You can save your bitmap to a File and just put the File path to the Intent
I need to pass image fetched from url into another activity page. I'm able to do so foe textviews. But i got stuck how to pass image from one page to another in android. dynamically through url.
can anyone give some suggestions regarding this.
1.First Way
Bitmap implements Parcelable
For Sent
Intent intent = new Intent(this, BActivity.class);
intent.putExtra("image", bitmap);
and Retrieve :
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("image");
2.Second Way
//Convert to byte array for sent
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 Retrieve in another activity:
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
By using intent you can pass image url. from there you have to display that. You can not pass Image.
send data using intent
Intent openNewActivity = new Intent(getApplicationContext(), Activity2.class);
openNewActivity.putExtra("image", "www.fb.com/profilepic.png");
startActivity(openNewActivity);
get data from your Activity2.class file
write this code in your onCreate method
Bundle extras = getIntent().getExtras();
String image;
if (extras != null) {
image = extras.getString("image");
// and get whatever type user account id is
}
now set image url on Image.
Bitmap r = imagelist[args.Position].Data;
byte[] arr = getByteFromBitmap(r);
var activity2 = new Intent(this, typeof(FullScreenImageViewActivity));
activity2.PutExtra("FullImage", arr);
StartActivity(activity2);
I get the correct byte[] and put it in the intent. But it never takes me to the other activity so the code below won't be triggered.
byte[] b = Intent.GetByteArrayExtra("FullImage");
Bitmap bitmap = BitmapFactory.DecodeByteArray(b, 0, b.Length);
imageView.SetImageBitmap(bitmap);
Try this code.....
To Pass Intent..
byte[] arr = YOURBYTEARRAY;
Intent intent= new Intent(this, FullScreenImageViewActivity.class);
intent.PutExtra("FullImage", arr);
StartActivity(intent);
To Get Intent Value..
byte[] b = getIntent().getByteArrayExtra("FullImage")
You shouldn't be using typeof in the intent parameters.
It should go:
var activity2 = new Intent(this, FullScreenImageViewActivity.class);
You're using putExtra on a Bitmap, but trying to extract a byte array in the 2nd activity. This will not end well.