问题
I have a grid of many products in my app. when the user selects one of the item in the grid, I am starting a new activity as DIALOG box and display the item's name,quantity and image. But I cannot send the image source dynamically.
here is my code
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
//Toast.makeText(getApplicationContext(),((TextView) v.findViewById(R.id.grid_item_label)).getText(), Toast.LENGTH_SHORT).show();
Intent item_intent = new Intent(MainActivity.this, Item.class);
item_intent.putExtra("name",((TextView) v.findViewById(R.id.grid_item_label)).getText());
item_intent.putExtra("quantity",((TextView) v.findViewById(R.id.grid_item_quantity)).getText());
//my problem is here***************************************************
ImageView my_image = findViewById(R.id.grid_item_image).getDrawable();
item_intent.putExtra("image",my_image.getXXXXX());
//*********************************************************************
MainActivity.this.startActivity(item_intent);
}
});
What should I use to get the image source from ImageView?
回答1:
Use Bundle like
imageView.buildDrawingCache();
Bitmap image= imageView.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
image.setImageBitmap(bmp );
回答2:
You need to convert drawable into Bitmap using this solution: And then you can pass it to the next activity via intent, because Bitmap class implements Parcelable interface.
回答3:
first you'll have to save your image first and then you can use this method to send the file name of your saved image
void Send() {
if (file != null) {
Intent intent = new Intent(this, Draw.class);
intent.putExtra(PICTURE_FILE_ID, file);
startActivity(intent);
} else if (file == null) {
Toast tst = Toast.makeText(getApplication(),
"Please Click Save First", Toast.LENGTH_SHORT);
tst.setGravity(Gravity.CENTER, 0, 0);
tst.show();
}
}
the other activity
use mBitmapFile = (File) getIntent().getSerializableExtra(
YourClassName.PICTURE_FILE_ID);
回答4:
You can put Parcable (bitmap for example) or serializable objects in a bundle and retrieve the object from the Bundle in the other activity.
how do you pass images (bitmaps) between android activities using bundles?
Although I would not recommend it because you pass a lot of data between activities. I would recommend you to pass the image reference id to the next activity and retrieve the image there.
How to pass the selectedListItem's object to another activity?
EDIT:
intent.putExtra("imageId", imageId);
in the other activity:
int imageRef = getIntent().getIntExtra("imageId", defaultValue);
http://developer.android.com/reference/android/content/Intent.html#getIntExtra%28java.lang.String,%20int%29
回答5:
When passing around Bitmap data between Activities, I generally prefer to store it in an extended Application class, which you have access to from all your Activities. You can obviously do it as the others have said, by passing it on the Intent, but if you need it in more than a couple of Activities, storing it in the Application gives you more flexibility.
回答6:
You can convert your drawable to bitmap then into byte array and pass this byte array with intent.
Drawable d;
Bitmap b= ((BitmapDrawable)d).getBitmap();
int bytes = b.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
b.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
user this code :
calling activity...
Intent i = new Intent(this, NextActivity.class);
Bitmap b; // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());
startActivity(i);
...and in your receiving activity
if(getIntent().hasExtra("byteArray")) {
ImageView previewThumbnail = new ImageView(this);
Bitmap b = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
}
回答7:
i had the same problem and solve it with passing the position of selected item via intent. as i think and read from professionals in stack its the best solution. just do that in FirstActivity.java that Contains GridView
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(),DetailActivity.class);
intent.putExtra("id",position);
startActivity(intent); } });
and then in your destination Activity you must get the position from extras and extract it from your database or where your images array are cast:
Intent intent = getActivity().getIntent();
int position = intent.getIntExtra("id",0);
ImageAdapter imageAdapter = new ImageAdapter(getActivity());
ImageView imageView = (ImageView)rootView.findViewById(R.id.movie_detail_image);
imageView.setImageResource((int)imageAdapter.getItem(position));
you must edit your image adapter getItem method to return selected image id.
来源:https://stackoverflow.com/questions/17878907/get-imageviews-image-and-send-it-to-an-activity-with-intent