android passing values to another activity but cannot read - android

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?

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