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);
Related
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);
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.
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 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);
I am trying to pass values to a new activity but cannot read the values from the new activity.
here is my code,
Intent myIntent = new Intent(Photo2Activity.this, MainActivity.class);
myIntent.putExtra("image1", image1);
myIntent.putExtra("image2", image2);
myIntent.putExtra("image3", image3);
myIntent.putExtra("konteyner_no", _txt_konteyner_id.getText().toString());
myIntent.putExtra("mahalle", _txt_mahalle.getText().toString());
myIntent.putExtra("sokak", _txt_sokak.getText().toString());
myIntent.putExtra("konteyner_temizmi", _check_konteyner_temizmi.isChecked());
myIntent.putExtra("yaninda_cop_varmi", _check_yaninda_cop_varmi.isChecked());
myIntent.putExtra("aralarinda_cop_varmi", _check_aralarinda_cop_vardi.isChecked());
myIntent.putExtra("zamansiz_cop_varmi", _check_zamansiz_cop_vardi.isChecked());
myIntent.putExtra("cop_obekleri_vardi", _check_cop_obekleri_vardi.isChecked());
myIntent.putExtra("note", _txt_note.getText().toString());
startActivity(myIntent);
how do I read them from the new activity(MainActivity)?
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(MainActivity.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);
Passing primitive types check the link below
How do you pass a string from one activity to another?