在Android上下载imageview的图像
我看到了这个问题:在Android上下载imageview的图像,android,download,android-imageview,Android,Download,Android Imageview,我看到了这个问题: 它不能解决我的问题,因为它只显示了在您已经拥有位图之后如何显示位图 我正在尝试从URL下载图像,以便在Android设备上用ImageView显示它。我不知道该怎么做 我在互联网上浏览了一下,这是我目前掌握的代码: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //S
它不能解决我的问题,因为它只显示了在您已经拥有位图之后如何显示位图 我正在尝试从URL下载图像,以便在Android设备上用ImageView显示它。我不知道该怎么做 我在互联网上浏览了一下,这是我目前掌握的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set local image
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test2);
//Prepare to download image
URL url;
InputStream in;
//BufferedInputStream buf;
try {
url = new URL("http://i.imgur.com/CQzlM.jpg");
in = url.openStream();
out = new BufferedOutputStream(new FileOutputStream("testImage.jpg"));
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
out.close();
in.close();
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
image.setImageBitmap(bMap);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
}
如果您在主线程上下载图像,代码可能会起作用。这意味着,当下载时间超过5秒时,你将看到著名的ANR对话框,你的应用程序将崩溃 您应该在后台线程中下载图像,并将结果发布回主线程。回到主线程,您可以使用下载的图像更新imageview 下面是一个例子:
package nl.entreco.stackoverflow;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
public class StackOverflowActivity extends Activity {
//
private ImageView mImageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Find the reference to the ImageView
mImageView = (ImageView) findViewById(R.id.test_image);
// You can set a temporary background here
//image.setImageResource(null);
// Start the DownloadImage task with the given url
new DownloadImage().execute("http://i.imgur.com/CQzlM.jpg");
}
/**
* Simple functin to set a Drawable to the image View
* @param drawable
*/
private void setImage(Drawable drawable)
{
mImageView.setBackgroundDrawable(drawable);
}
public class DownloadImage extends AsyncTask<String, Integer, Drawable> {
@Override
protected Drawable doInBackground(String... arg0) {
// This is done in a background thread
return downloadImage(arg0[0]);
}
/**
* Called after the image has been downloaded
* -> this calls a function on the main thread again
*/
protected void onPostExecute(Drawable image)
{
setImage(image);
}
/**
* Actually download the Image from the _url
* @param _url
* @return
*/
private Drawable downloadImage(String _url)
{
//Prepare to download image
URL url;
BufferedOutputStream out;
InputStream in;
BufferedInputStream buf;
//BufferedInputStream buf;
try {
url = new URL(_url);
in = url.openStream();
/*
* THIS IS NOT NEEDED
*
* YOU TRY TO CREATE AN ACTUAL IMAGE HERE, BY WRITING
* TO A NEW FILE
* YOU ONLY NEED TO READ THE INPUTSTREAM
* AND CONVERT THAT TO A BITMAP
out = new BufferedOutputStream(new FileOutputStream("testImage.jpg"));
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
out.close();
in.close();
*/
// Read the inputstream
buf = new BufferedInputStream(in);
// Convert the BufferedInputStream to a Bitmap
Bitmap bMap = BitmapFactory.decodeStream(buf);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
return new BitmapDrawable(bMap);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
return null;
}
}
}
包nl.enterco.stackoverflow;
导入java.io.BufferedInputStream;
导入java.io.BufferedOutputStream;
导入java.io.FileOutputStream;
导入java.io.InputStream;
导入java.net.URL;
导入android.app.Activity;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.drawable.BitmapDrawable;
导入android.graphics.drawable.drawable;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.ImageView;
公共类StackOverflowActivity扩展活动{
//
私有图像视图mImageView;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//查找对ImageView的引用
mImageView=(ImageView)findViewById(R.id.test\u图像);
//您可以在此处设置临时背景
//image.setImageResource(空);
//使用给定的url启动DownloadImage任务
新建下载映像()。执行(“http://i.imgur.com/CQzlM.jpg");
}
/**
*简单函数,用于将可绘制图像设置为图像视图
*@param可绘制
*/
私有void setImage(可绘制)
{
mImageView。后退可拉深(可拉深);
}
公共类DownloadImage扩展异步任务{
@凌驾
受保护的可抽出式doInBackground(字符串…arg0){
//这是在后台线程中完成的
返回下载图像(arg0[0]);
}
/**
*下载图像后调用
*->这将再次调用主线程上的函数
*/
受保护的void onPostExecute(可绘制图像)
{
设置图像(图像);
}
/**
*实际上是从_url下载图像
*@param\u url
*@返回
*/
私有可绘制下载图像(字符串\u url)
{
//准备下载图像
网址;
缓冲输出流输出;
输入流输入;
缓冲输入流buf;
//缓冲输入流buf;
试一试{
url=新url(_url);
in=url.openStream();
/*
*这是不必要的
*
*你试着在这里创建一个真实的图像,通过书写
*添加到新文件
*您只需要读取INPUTSTREAM
*并将其转换为位图
out=新的BufferedOutputStream(新的FileOutputStream(“testImage.jpg”);
int i;
而((i=in.read())!=-1){
写出(i);
}
out.close();
in.close();
*/
//读取输入流
buf=新的BufferedInputStream(in);
//将BufferedInputStream转换为位图
位图bMap=BitmapFactory.decodeStream(buf);
if(in!=null){
in.close();
}
如果(buf!=null){
buf.close();
}
返回新的BitmapDrawable(bMap);
}捕获(例外e){
Log.e(“读取文件时出错”,e.toString());
}
返回null;
}
}
}
别忘了将permission.INTERNET添加到您的清单中,否则,您将得到一个错误…我尝试了这个。它不会给出错误,但不会加载图像。Internet权限存在于AndroidManifest.xml文件中。这可能与DownloadImage类在另一个类中有关吗?不管怎样,我对代码做了一些修改,它成功了。在函数setImage中,我将唯一一行更改为:
mImageView.setImageDrawable(drawable)代码>是的,很抱歉让你久等了。测试后忘记了这一点。不推荐使用setBackgoundDrawable函数,改为setBackground。如果找到答案,您应该接受它。这样,其他用户就知道它是有效的。祝你好运