如何从网址下载图片?

时间:2014-01-24 15:38:07

标签: android android-asynctask android-image javax.imageio android-bitmap

我使用此代码:

private class AsyncGetCatalogs extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Map<String, String> catalogsBuffer = Requester.getCatalog();
            catalogsNumber = catalogsBuffer.size();
            catalogs = new catalogInfo[catalogsNumber];
            int n = 0;
            for(Map.Entry<String, String> entry : catalogsBuffer.entrySet()) {
                catalogs[n] = new catalogInfo();
                String name = entry.getKey();
                String link = entry.getValue();
                String imageLink = Requester.getCatalogImageLink(name);
                byte imgBuf[] = Requester.getCatalogImage(imageLink);
                Bitmap bitmap = BitmapFactory.decodeByteArray(imgBuf, 0, imgBuf.length);
                catalogs[n].setBitmap(bitmap);
                catalogs[n].setCatLink(link);
                catalogs[n].setCatName(name);
                n++;

            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override 
    protected void onPostExecute(Void result) {
        try {
            LinearLayout catsLayouts[] = new LinearLayout[catalogsNumber];
            for(int i=0;i<catalogsNumber;i++) {
                catsLayouts[i] = new LinearLayout(getApplicationContext());
                ImageView catImageView = new ImageView(getApplicationContext());
                catImageView.setImageBitmap(catalogs[i].getImage());
                catsLayouts[i].addView(catImageView);
                mainLayout.addView(catsLayouts[i]);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}

应用程序崩溃:

byte imgBuf[] = Requester.getCatalogImage(imageLink);

有这样的堆栈跟踪;

01-25 00:22:26.998: E/AndroidRuntime(975): FATAL EXCEPTION: AsyncTask #2
01-25 00:22:26.998: E/AndroidRuntime(975): java.lang.RuntimeException: An error occured while executing doInBackground()
01-25 00:22:26.998: E/AndroidRuntime(975):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
01-25 00:22:26.998: E/AndroidRuntime(975):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-25 00:22:26.998: E/AndroidRuntime(975):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-25 00:22:26.998: E/AndroidRuntime(975):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-25 00:22:26.998: E/AndroidRuntime(975):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-25 00:22:26.998: E/AndroidRuntime(975):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-25 00:22:26.998: E/AndroidRuntime(975):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-25 00:22:26.998: E/AndroidRuntime(975):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-25 00:22:26.998: E/AndroidRuntime(975):  at java.lang.Thread.run(Thread.java:856)
01-25 00:22:26.998: E/AndroidRuntime(975): Caused by: java.lang.NoClassDefFoundError: javax.imageio.ImageIO
01-25 00:22:26.998: E/AndroidRuntime(975):  at IrkroseRequester.IrkroseRequester.getCatalogImage(IrkroseRequester.java:152)
01-25 00:22:26.998: E/AndroidRuntime(975):  at com.example.client.MainContentActivity$AsyncGetCatalogs.doInBackground(MainContentActivity.java:79)
01-25 00:22:26.998: E/AndroidRuntime(975):  at com.example.client.MainContentActivity$AsyncGetCatalogs.doInBackground(MainContentActivity.java:1)
01-25 00:22:26.998: E/AndroidRuntime(975):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-25 00:22:26.998: E/AndroidRuntime(975):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

** getCatalog Image **

public byte[] getCatalogImage(String _link) throws IOException {
    String link = "http://irkrose.ru/images/catalogImages/"+_link;
    URL url = new URL(_link);
    BufferedImage img = ImageIO.read(url);
    ByteArrayOutputStream res = new ByteArrayOutputStream();
    ImageIO.write(img, "jpg", res);
    return res.toByteArray();
}

据我所知,它无法找到ImageIO。我知道android没有它但是所有使用ImageIO的函数都在另一个java包中,只是作为一个lib添加到app。 Android应用程序仅适用于字节数组。

那有什么不对?如何从URL以另一种方式加载.jpg图像?并且它可能是异步类的问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

private class GetImageTaskextends AsyncTask<String, Void, Bitmap> {
    ImageView tempBitmapImage;

    public GetImageTask(ImageView tempBitmapImage) {
        this.tempBitmapImage = tempBitmapImage;
    }

    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) {
        tempBitmapImage.setImageBitmap(result);
    }
}

并像这样使用它:

new GetImageTask((ImageView) imgIcon).execute("www.test.com/test.jpg");

我希望它有所帮助。