Android的图像抓取 [英] Android image fetching
问题描述
什么是来从URL的图像在Android程序中最简单的方法是什么?
What is the simplest way to fetch an image from a url in an android program?
推荐答案
我会强烈建议使用的 AsyncTask的代替。我最初使用 URL。的OpenStream ,但它有问题。
I would strongly recommend using an AsyncTask instead. I originally used URL.openStream, but it has issues.
class DownloadThread extends AsyncTask<URL,Integer,List<Bitmap>>{
protected List<Bitmap> doInBackground(URL... urls){
InputStream rawIn=null;
BufferedInputStream bufIn=null;
HttpURLConnection conn=null;
try{
List<Bitmap> out=new ArrayList<Bitmap>();
for(int i=0;i<urls.length;i++){
URL url=urls[i];
url = new URL("http://mysite/myimage.png");
conn=(HttpURLConnection) url.openConnection()
if(!String.valueOf(conn.getResponseCode()).startsWith('2'))
throw new IOException("Incorrect response code "+conn.getResponseCode()+" Message: " +getResponseMessage());
rawIn=conn.getInputStream();
bufIn=new BufferedInputStream();
Bitmap b=BitmapFactory.decodeStream(in);
out.add(b);
publishProgress(i);//Remove this line if you don't want to use AsyncTask
}
return out;
}catch(IOException e){
Log.w("networking","Downloading image failed");//Log is an Android specific class
return null;
}
finally{
try {
if(rawIn!=null)rawIn.close();
if(bufIn!=null)bufIn.close();
if(conn!=null)conn.disconnect();
}catch (IOException e) {
Log.w("networking","Closing stream failed");
}
}
}
}
关闭流/连接和异常处理是难以在这种情况下。据 Sun文档你应该只需要但是关闭最外层流,< A HREF =http://stackoverflow.com/questions/884007/best-way-to-close-nested-streams-in-java>这似乎是更复杂。不过,我将结束最内侧的流首先要保证,如果我们不能关闭的BufferedInputStream
它是封闭的。
Closing the stream/connection and exception handling is difficult in this case. According to Sun Documentation you should only need to close the outermost stream, however it appears to be more complicated. However, I am closing the inner most stream first to ensure it is closed if we can't close the BufferedInputStream
.
我们关闭了最后,这样的异常没有prevent它们关闭。我们占流的可能性将是空
如果有异常$ P $被初始化pvented他们。如果我们有关闭的过程中的异常,我们只需登录并忽略这一点。即使这样,如果发生运行时错误可能无法正常工作。
We close in a finally so that an exception doesn't prevent them being closed. We account for the possibility of the streams will being null
if an exception prevented them from being initialised. If we have an exception during closing, we simply log and ignore this. Even this might not work properly if a runtime error occurs .
您可以使用的AsyncTask
类,如下所示。开始于在preExecute
的动画。更新 onProgressUpdate
的进展情况。 onPostExecute
应该处理的实际图像。使用 OnCancel的
来允许用户取消操作。与 AsyncTask.execute
开始吧。
You can use the AsyncTask
class as follows. Start an animation in onPreExecute
. Update the progress in onProgressUpdate
. onPostExecute
should handle the actual images. Use onCancel
to allow the user to cancel the operation. Start it with AsyncTask.execute
.
值得一提的是,<一个href="http://www.google.com/$c$csearch/p?hl=en#uX1GffpyOZk/core/java/android/os/AsyncTask.java&d=3"相对=nofollow>来源$ C $ C 和许可证允许我们使用类的非Android项目。
It is worth noting that the source code and the license allow us to use the class in non-Android projects.
这篇关于Android的图像抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!