用于在模拟器和物理设备上运行的ImageView上显示URL中位图的正确方法?

问题描述 投票:0回答:1

我正在构建用于本地媒体的Android应用并解析相关图像,例如通过Movie Database API,一切在Android Studio模拟器上都可以正常工作,但是当我构建apk并在真实设备上安装时,所有下载的图像都没有显示在其ImageViews上。

我正在使用的代码是;

String Url = Actionlist1.getString(t);//pasrsed from a JSONArray object
java.net.URL url = new java.net.URL(Url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap b = BitmapFactory.decodeStream(input);
Bitmap b1 = Bitmap.createScaledBitmap(b, 100, 140, false);//hard coded scaling to the ImageView width and height.
ImageView B1 = findViewById(R.id.Button1);
B1.setImageBitmap(b1);

[此代码在模拟器上运行时工作正常,但是在设备上运行时,所有图像均为黑色,我不知所措,因为代码应该可以正常工作,而不是缺少缺少图像的应用程序行为正常。

android bitmap imageview textures drawable
1个回答
0
投票
尝试一下

Glide.with(this) .asBitmap() .load(//url) .placeholder(R.drawable.vijay) .into(imageView);

还添加此依赖项
    implementation 'com.github.bumptech.glide:glide:4.9.0'

0
投票
只需将此行添加到您的app.gradle文件中

implementation 'com.squareup.picasso:picasso:2.71828'

之后,您可以轻松地从在线加载图像。

Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);

© www.soinside.com 2019 - 2024. All rights reserved.