小智 700
来自Android开发者:
// show The Image in a ImageView
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
public void onClick(View v) {
startActivity(new Intent(this, IndexActivity.class));
finish();
}
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)
确保您在AndroidManifest.xml访问互联网时设置了以下权限.
<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)
chi*_*ada 159
1. 毕加索允许在您的应用程序中轻松加载图像 - 通常只需一行代码!
使用Gradle:
implementation 'com.squareup.picasso:picasso:2.71828'
Run Code Online (Sandbox Code Playgroud)
只需一行代码!
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
Run Code Online (Sandbox Code Playgroud)
2. Glide Android的图像加载和缓存库专注于平滑滚动
使用Gradle:
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}
Run Code Online (Sandbox Code Playgroud)
//对于一个简单的视图:
Glide.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Run Code Online (Sandbox Code Playgroud)
3. fresco 是一个功能强大的系统,用于在Android应用程序中显示图像.Fresco负责图像加载和显示,因此您不必这样做.
Ste*_*eve 150
你必须先下载图像
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
然后使用Imageview.setImageBitmap将位图设置为ImageView
kou*_*ush 70
我写了一个类来处理这个问题,因为它似乎是我各种项目中反复出现的需求:
https://github.com/koush/UrlImageViewHelper
UrlImageViewHelper将使用在URL处找到的图像填充ImageView.
该示例将执行Google图片搜索并异步加载/显示结果.
UrlImageViewHelper将自动下载,保存和缓存BitmapDrawables的所有图像URL.重复的网址不会被加载到内存中两次.位图内存通过使用弱引用哈希表进行管理,因此只要您不再使用该图像,就会自动对其进行垃圾回收.
Pra*_*een 65
无论如何,人们要求我的评论将其作为答案发布.我发布了.
URL newurl = new URL(photo_url_str);
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
profile_photo.setImageBitmap(mIcon_val);
Run Code Online (Sandbox Code Playgroud)
谢谢.
Kyl*_*egg 63
如果您基于按钮单击加载图像,则上面接受的答案很好,但是如果您在新活动中执行此操作,则会将UI冻结一两秒钟.环顾四周,我发现一个简单的asynctask消除了这个问题.
要使用asynctask,请在活动结束时添加此类:
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()方法调用:
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute(MY_URL_STRING);
Run Code Online (Sandbox Code Playgroud)
结果是快速加载的活动和图像视图,根据用户的网络速度显示一瞬间.
Blu*_*ell 25
您还可以使用此LoadingImageView视图从URL加载图像:
http://blog.blundellapps.com/imageview-with-loading-spinner/
从该链接添加类文件后,您可以实例化URL图像视图:
在xml中:
<com.blundell.tut.LoaderImageView
android:id="@+id/loaderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
image="http://developer.android.com/images/dialog_buttons.png"
/>
Run Code Online (Sandbox Code Playgroud)
在代码中:
final LoaderImageView image = new LoaderImageView(this, "http://developer.android.com/images/dialog_buttons.png");
Run Code Online (Sandbox Code Playgroud)
并使用以下方式更新它
image.setImageDrawable("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
Run Code Online (Sandbox Code Playgroud)
小智 10
public class LoadWebImg extends Activity {
String image_URL=
"http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView bmImage = (ImageView)findViewById(R.id.image);
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bm = LoadImage(image_URL, bmOptions);
bmImage.setImageBitmap(bm);
}
private Bitmap LoadImage(String URL, BitmapFactory.Options options)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try{
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
}
catch (Exception ex)
{
}
return inputStream;
}
}
Run Code Online (Sandbox Code Playgroud)
Abh*_*nde 10
嗨,我有最简单的代码试试这个
public class ImageFromUrlExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
imgView.setImageDrawable(drawable);
}
private Drawable LoadImageFromWebOperations(String url)
{
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
main.xml中
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/ImageView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
试试这个
我最近在这里找到了一个线程,因为我必须对带有图像的列表视图做类似的事情,但原理很简单,因为你可以在那里显示的第一个示例类中读取(通过jleedev).你得到图像的输入流(来自网络)
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
Run Code Online (Sandbox Code Playgroud)
然后将图像存储为Drawable,然后将其传递给ImageView(通过setImageDrawable).再次从上面的代码片段看一下整个线程.
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
Run Code Online (Sandbox Code Playgroud)
对我来说,这个任务最好的现代图书馆是Picasso by Square.它允许通过URL使用单行将图像加载到ImageView:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Run Code Online (Sandbox Code Playgroud)
这里有很多好消息...我最近发现了一个名为SmartImageView的类,到目前为止看起来效果非常好.非常容易合并和使用.
http://loopj.com/android-smart-image-view/
https://github.com/loopj/android-smart-image-view
更新:我最后写了一篇关于此的博客文章,因此请查看有关使用SmartImageView的帮助.
第二次更新:我现在总是使用Picasso(见上文)并强烈推荐它.:)
小智 7
这是一个迟到的回复,正如上面建议的那样,AsyncTask谷歌搜索后我发现了另一种解决这个问题的方法.
Drawable drawable = Drawable.createFromStream((InputStream) new URL("url").getContent(), "src");
imageView.setImageDrawable(drawable);
这是完整的功能:
public void loadMapPreview () {
//start a background thread for networking
new Thread(new Runnable() {
public void run(){
try {
//download the drawable
final Drawable drawable = Drawable.createFromStream((InputStream) new URL("url").getContent(), "src");
//edit the view in the UI thread
imageView.post(new Runnable() {
public void run() {
imageView.setImageDrawable(drawable);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
Run Code Online (Sandbox Code Playgroud)
不要忘记在您AndroidManifest.xml的访问互联网中添加以下权限.
<uses-permission android:name="android.permission.INTERNET" />
我自己尝试过,我还没有遇到任何问题.
imageView.setImageBitmap(BitmapFactory.decodeStream(imageUrl.openStream()));//try/catch IOException and MalformedURLException outside
Run Code Online (Sandbox Code Playgroud)
小智 5
这会对你有所帮助......
定义imageview并将图像加载到其中.....
Imageview i = (ImageView) vv.findViewById(R.id.img_country);
i.setImageBitmap(DownloadFullFromUrl(url));
Run Code Online (Sandbox Code Playgroud)
然后定义此方法:
public Bitmap DownloadFullFromUrl(String imageFullURL) {
Bitmap bm = null;
try {
URL url = new URL(imageFullURL);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
bm = BitmapFactory.decodeByteArray(baf.toByteArray(), 0,
baf.toByteArray().length);
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
return bm;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
522698 次 |
| 最近记录: |