I have a grid view when i click an item inside the grid view and its going to another activity and view it there my code something like this :
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), MainActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
Activity which is im going to view
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("s");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(imageAdapter.moodpic[position]);
You try to get Extra with wrong key here getInt("s").
Try this
int position = getIntent().getIntExtra("id",0);
you should change with
int position = i.getExtras().getInt("id" , 0);
instead this
int position = i.getExtras().getInt("s");
This is because you have put "id" as a key so whenever you are retrieving then that key must be same and by default the value of int is 0.
I would highly recommend a different approach.
It's possible if you REALLY want to do it, but it costs a lot of memory and is also slow. It might not work if you have an older phone and a big bitmap. You could just pass it as an extra, for example
intent.putExtra("data", bitmap)
A Bitmap implements Parcelable, so you can put it in an extra. Likewise, a bundle has putParcelable f you want to pass it inbetween activities, I would store it in a file. That's more efficient, and less work for you. You can create private files in your data folder using MODE_PRIVATE that are not accessible to any other app.
First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.
Convert Bitmap to Byte Array:-
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
// Bitmap bmp = BitmapFactory.decodeFile(path); You can use this also.
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);
2) First Save image into SDCard and in next activity set this image into ImageView.
3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.
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);
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);
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 making an application in Android, which requires sending bitmap object from one activity and displaying that bitmap object sent on the second activity page. But , I am getting a blank screen
Here is a sample of my code for sending the Bitmap object :-
Intent intent = new Intent(Display2.this, Display3.class);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
intent.putExtra("BitmapImage", bitmap);
startActivity(intent);
Now the part of code on second activity to retrieve the sent object and displaying it on screen :-
public class Display3 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("BitmapImage");
ImageView myIV = (ImageView) findViewById(R.id.imageView1);
bitmap = bitmap.createBitmap(myIV.getWidth(), myIV.getHeight(), Bitmap.Config.RGB_565);
myIV.setImageBitmap(bitmap);
setContentView(R.layout.display3);
}
}
can anyone suggest whats wrong in this part ?
Thanks !
Bitmap implements Parcelable object, so you could always pass it in the intent like below :
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it on the other end:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
this is not good for passing bitmap from one activity to another..
You can simply name you Bitmap as static first.
then create a method like
public static Bitmap getBitmap(){
return bitmap;
}
then you can simply call from other activities,
bitmapexistingclass.getBitmap();
if we use intents for passing bitmap we will get some errors check this question How to pass bitmap from one activity to another
from your code above you are creating an empty bitmap:
ImageView iv = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
I do not see any reason why from this code you'll get anything else then blank black bitmap with hight and width equals to the imageView hight and width
what you are expecting from android to fill drawing in Bitmap itself , you are creating an Empty Bitmap Object with height and width equavalent to ImageView object;
as Bitmap implements Parcelable you can simply put it in bundle and retrive on other Activity
Use below two methods to convert a bitmap to string and vice versa. Then you can pass the string with intents from activity to another activity. I too do the same in my application.
Then we dont need to use ParcelableExtra and serialzable class.
public String convertBitmapToString(Bitmap src) {
if(src!= null){
ByteArrayOutputStream os=new ByteArrayOutputStream();
src.compress(android.graphics.Bitmap.CompressFormat.PNG, 100,(OutputStream) os);
byte[] byteArray = os.toByteArray();
return Base64.encodeToString(byteArray,Base64.DEFAULT);
}
return null;
}
public Bitmap getBitMapFromString(String src){
Bitmap bitmap = null;
if(src!= null){
byte[] decodedString = Base64.decode(src.getBytes(), Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
return bitmap;
}
return null;
}