简体   繁体   English

如何使用“ setImageURI”功能将图像从数据库加载到android ImageView中?

[英]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.

相关问题 Android ImageView.setImageURI缩放图像 - Android ImageView.setImageURI scales image ImageView.setImageURI方法未在Android上显示图像 - ImageView.setImageURI method does not shown image on android 如何从android imageview中的dataurl加载图像? - How to load an image from dataurl in android imageview? 无法使用普通的setImageUri和Picasso - Android Nougat在ImageView上设置图像 - Not able to set image on ImageView with normal setImageUri and with Picasso - Android Nougat Android imageView.setImageUri 与专辑图像 Uri 上的安全异常 - Android Security Exception on imageView.setImageUri with album image Uri 如何从类路径加载Android ImageView的图像? - How to load image for Android ImageView from classpath? 如何从imageView在Android数据库中添加图片 - How to strome image in Android Database from imageView 如何在android imageview中显示数据库中的图像 - How to display the image from database in android imageview 如何从mysql数据库加载图像并在android中的imageview中显示? - How to Load Image form mysql database and display in imageview in android? 在 Android(java) 中,如何使用 URL 而不是来自互联网的图像地址在 ImageView 中加载图像? - In Android(java), How to load Image in ImageView by using URL instead of image addresses from the internet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM