[英]How to load image from database into android ImageView using “setImageURI” function?
I have to set image in Image view. 我必须在“图像”视图中设置图像。 I used Glide library to set image in recycler view, now when I click to recycler view item I want to set it into another activity.
我使用Glide库在回收器视图中设置图像,现在当我单击回收器视图项时,我想将其设置为另一个活动。
I just take image url through intent and store it as a string variable. 我只是通过意图获取图像url并将其存储为字符串变量。
final String image = intent.getStringExtra("Image");
Then I tried to set image using the following line of code. 然后,我尝试使用以下代码行设置图像。
displayImage.setImageURI(Uri.parse(image));
Note: displayImage is my targeted imageview. 注意:displayImage是我的目标imageview。
And I could get the correct url here. 我可以在这里获得正确的网址。
You can not display image from URL
using setImageURI();
您不能使用
setImageURI();
从URL
显示图像setImageURI();
You need to pass url to your activity than display it using Glide 您需要将url传递给活动,而不是使用Glide显示它
Use this 用这个
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_placeholder);
requestOptions.error(R.drawable.ic_error);
Glide.with(this)
.load(image)
.apply(requestOptions)
.into(displayImage);
Instead of this 代替这个
displayImage.setImageURI(Uri.parse(image));
Well when you have already set image once in RecyclerView
why not use Bitmap
from that ImageView
. 好吧,如果您已经在
RecyclerView
设置了一次图像,为什么不使用该ImageView
Bitmap
。 This will save network data and time. 这样可以节省网络数据和时间。
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bs);
intent.putExtra("byteArray", bs.toByteArray());
startActivity(intent);
Then in target activity 然后在目标活动中
if (getIntent().hasExtra("byteArray")) {
Bitmap bitmap =
BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length);
imageView.setImageBitmap(bitmap);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.