Jig*_*iya 57 url https android image
我想在屏幕上显示图像.图像应来自URL,而不是可绘制的.
代码在这里:
<ImageView android:id="@+id/ImageView01" android:src = "http://l.yimg.com/a/i/us/we/52/21.gif"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
Run Code Online (Sandbox Code Playgroud)
但它在编译时出错.
如何在Android中显示来自URL的图像?
Chi*_*rag 97
您可以直接在网上显示图像而无需下载.请检查以下功能.它会将网络上的图像显示在图像视图中.
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用活动中的代码将图像设置为imageview.
Sat*_*omu 17
我试过这个代码为我工作,直接从url获取图像
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Run Code Online (Sandbox Code Playgroud)
在onCreate()方法中使用
新的DownloadImageTask((ImageView)findViewById(R.id.image)).execute(" http://scoopak.com/wp-content/uploads/2013/06/free-hd-natural-wallpapers-download-for-pc .jpg ");
DzM*_*ter 15
您可以尝试这个我在另一个问题中找到的.
Android,在URL上创建一个等于ImageView图像的图像
try {
ImageView i = (ImageView)findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
你可以试试Picasso,这真的很好,也很容易.不要忘记在清单中添加权限.
Picasso.with(context)
.load("http://ImageURL")
.resize(width,height)
.into(imageView );
Run Code Online (Sandbox Code Playgroud)
举个简单的例子,
http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device
您将不得不使用httpClient并下载图像(如果需要,将其缓存),
提供用于在listview中显示图像的解决方案,基本上是相同的代码(检查从url设置imageview的代码)以进行显示.
| 归档时间: |
|
| 查看次数: |
194281 次 |
| 最近记录: |