如何在android中声明多个图像url

如何在android中声明多个图像url,android,imageview,Android,Imageview,如何动态声明多个图像URL和我希望显示选定的一个图像而不是显示所有图像。在服务器中检索到的图像 我尝试了这段代码,但只从服务器检索到一个映像。我需要知道如何声明和检索多个图像,同样的代码可以声明图像 编码如下 @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMen

如何动态声明多个图像URL和我希望显示选定的一个图像而不是显示所有图像。在服务器中检索到的图像

我尝试了这段代码,但只从服务器检索到一个映像。我需要知道如何声明和检索多个图像,同样的代码可以声明图像

编码如下

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class Image_Async extends AsyncTask<Bitmap,Bitmap,Bitmap>
{
    @Override
    protected Bitmap doInBackground(Bitmap... arg0) {
        // TODO Auto-generated method stub
        Bitmap  bmp=    null;
        try {
            URL url =
 new URL("http://fc02.deviantart.net/fs42/i/2009/132/7/2/DIWA_boy_animation_by_LunarRain27.jpg");
            try {

            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bmp;
    }
@覆盖
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
类映像\u异步扩展异步任务
{
@凌驾
受保护位图doInBackground(位图…arg0){
//TODO自动生成的方法存根
位图bmp=null;
试一试{
网址=
新网址(“http://fc02.deviantart.net/fs42/i/2009/132/7/2/DIWA_boy_animation_by_LunarRain27.jpg");
试一试{
bmp=BitmapFactory.decodeStream(url.openConnection().getInputStream());
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回bmp;
}

有人给你提建议吗…

嗯,我不知道你到底想要什么

如果您希望能够缓存图像(一次下载多个并显示一个),请使用通用图像加载器

如果您只是想下载多个图像,那么不要解码位图,而是打开输入流并写入文件

try {
    final File file = new File(getCacheDir(), "imagename goes here");
    final OutputStream output = new FileOutputStream(file);
    try {
        try {
            final byte[] buffer = new byte[1024];
            int read;
            // this is the input stream from your url connection
            while ((read = inputstream.read(buffer)) != -1)
                output.write(buffer, 0, read);

            output.flush();
        } finally {
            output.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
} finally {
    inputstream.close();
}
因为你不会无缘无故地想要在RAM中有那么多的图像(但也许你确实有一个原因

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
无论如何,您在一个循环中运行上面的其中一个,在一个后台线程上运行尽可能多的图像,让它在完成时通知您

class EZAsyncTask extends AsyncTask<Void, Void, List<Bitmap>>
{

    private AsyncTaskListener listener;
    private List<String> urlList;

    public EZAsyncTask(List<String> urlList, AsyncTaskListener listener)
    {
        super();
        this.urlList = urlList;
        this.listener = listener;
    }

    @Override
    protected String doInBackground(Void... params)
    {
        List<Bitmap> bitmapList = new ArrayList<Bitmap>();
        for (String url : urlList)
        {
            bitmapList.add(getBitmapFromURL(url));
        }
        return bitmapList;
    }

    @Override
    protected void onPostExecute(List<Bitmap> bitmapList)
    {
        super.onPostExecute(bitmapList);
        listener.onCompletion(bitmapList);
    }

    public interface AsyncTaskListener
    {
        public void onCompletion(List<Bitmap> bitmapList);
    }

}
类EZAsyncTask扩展了AsyncTask { 私有异步任务侦听器; 私人名单; 公共EZAsyncTask(列表urlList、AsyncTaskListener侦听器) { 超级(); this.urlist=urlist; this.listener=listener; } @凌驾 受保护字符串doInBackground(无效…参数) { List bitmapList=新建ArrayList(); for(字符串url:urlList) { add(getBitmapFromURL(url)); } 返回位图列表; } @凌驾 受保护的void onPostExecute(列表位图列表) { super.onPostExecute(位图列表); onCompletion(位图列表); } 公共接口异步任务侦听器 { 完成时的公共作废(列表位图列表); } } 像这样使用它

urlList = new ArrayList<String>();
urlList.add("http://images.fastcompany.com/upload/Simple.jpg");

EZAsyncTask ezTask = new EZAsyncTask(urlList, new AsyncTaskListener() {
@Override
public void onCompletion(List<Bitmap> bitmapList)
{
    // TODO handle response
}
});
ezTask.execute();
urlist=newarraylist();
URL列表。添加(“http://images.fastcompany.com/upload/Simple.jpg");
EZAsyncTask ezTask=new EZAsyncTask(urlList,new AsyncTaskListener()){
@凌驾
完成时的公共void(列表位图列表)
{
//TODO句柄响应
}
});
ezTask.execute();

不管怎么说,这里面有很多。如果你能理解的话,试着去理解它,从长远来看,这会有很大帮助;)

创建一个URL字符串和位图数组

并逐个处理它们,将结果添加到每个URL上的位图数组中

问题..

假设您有很多带有位图的URL,它可能会导致OutOfMemoryException