问题
I am using volley to download images and text on a listview in my activity.
On clicking a row i pass both the image and the text to Details activity.
On details activity i would like to pass only the Image to yet another activity.
After research on S/O i found a code that works:
BitmapDrawable bd = (BitmapDrawable) ((NetworkImageView) view.findViewById(R.id.img2))
.getDrawable();
Bitmap bitmapzoom=bd.getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bd.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imgByte = baos.toByteArray();
Intent intent=new Intent(getApplicationContext(),ZoomImage.class);
intent.putExtra("image", imgByte);
startActivity(intent);
}
});
And on the next activity i retrieve it like:
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("image");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
BitmapDrawable background = new BitmapDrawable(bmp);
findViewById(R.id.img2).setBackgroundDrawable(background);
However the image is being placed as a background image.I would like to place the image in my ImageView.
Not as a background like the way it is done on the code above.
Currently setting the image on the ImageView has not been successfull.Any suggestions will be welcomed.
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
tools:context="com.softtech.stevekamau.buyathome.ZoomImage">
<ImageView
android:id="@+id/back"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/back"/>
<RelativeLayout
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<ImageView
android:id="@+id/img2"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
android:maxHeight="100dp"
android:maxWidth="50dp" />
</RelativeLayout>
</LinearLayout>
How it looks as a background:
回答1:
EDIT: To set the imageview actual image using a bitmap, not the bitmapdrawable:
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("image");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
findViewById(R.id.img2).setImageBitmap(bmp);
Careful in passing large images between activities, it might not show the image in the other activity. Better to save to internal or external memory and then get it on the other activity.
回答2:
get The Url of the image and pass it via bundle , later in activity you can use Picasso library to load it into view below is the code you may have a look.
private void setImageWithPicaso(String imageUrl) {
if (!(imageUrl == null)) {
Picasso.with(getActivity()).load(imageUrl).placeholder(R.drawable.placeholder_background).into(imageView, new Callback() {
@Override
public void onSuccess() {
spinner.setVisibility(View.GONE);
}
@Override
public void onError() {
spinner.setVisibility(View.GONE);
AppLog.showToastMessage(getActivity(), "Image loading failed!");
}
});
} else {
spinner.setVisibility(View.GONE);
AppLog.showToastMessage(getActivity(), "Image loading failed!");
}
}
回答3:
Substitute:
findViewById(R.id.img2).setBackgroundDrawable(background);
for:
findViewById(R.id.img2).setImageDrawable(background);
However the image is being placed as a background image.I would like to place the image in my ImageView. Not as a background like the way it is done on the code above.
I think I get where your confusion is coming from. Am not sure you quite understand what an ImageView is.Your image is infact IN your imageview, else you wouldn't be seeing it.
回答4:
I don't recommend that you pass the image data between activities.
The recommended way is to save it in disk, and pass the filepaths between activities.
来源:https://stackoverflow.com/questions/31637386/how-to-send-an-image-downloaded-by-volley-to-the-next-activity