Rid*_*ANI 3 url https android image
我使用此代码从 url 显示图像,它可以工作,但图像存储在设备中,这是问题所在,因为我只想在用户连接到互联网时显示此图像
ImageView imageView=(ImageView) findViewById(R.id.imageView3);
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)
如何在 Android 中显示来自 URL 的图像?
小智 7
尝试这个解决方案..
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(image);
Run Code Online (Sandbox Code Playgroud)
它会帮助你。
compile "com.squareup.picasso:picasso:2.4.0"
Run Code Online (Sandbox Code Playgroud)
使用此库加载图像。
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder) //optional
.resize(imgWidth, imgHeight) //optional
.centerCrop() //optional
.into(image); //Your image view object.
Run Code Online (Sandbox Code Playgroud)
要检查互联网连接,请使用以下代码
ConnectivityManager connectivity = (ConnectivityManager) _Context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo activeNetwork = connectivity.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
available = true;
}
}
}
}
if (available == false) {
NetworkInfo wiMax = connectivity.getNetworkInfo(6);
if (wiMax != null && wiMax.isConnected()) {
available = true;
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用下载图像
class DownloadTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
String image_name, studentName;
TextDrawable drawable;
public DownloadTask(ImageView bmImage, String file_name) {
this.bmImage = bmImage;
this.image_name = file_name;
}
protected Bitmap doInBackground(String... urls) {
InputStream input = new java.net.URL(image_name).openStream();
if(input.available()!=0) {
return BitmapFactory.decodeStream(input);
}
return null;
}
protected void onPostExecute(Bitmap result) {
try {
if(result == null)
bmImage.setImageDrawable(drawable);
else
bmImage.setImageBitmap(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19711 次 |
| 最近记录: |