Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to download an image and use it as new resource?

I want to download image from distant server and use it as resource. Is it possible ? How can I do this ?

like image 634
nonozor Avatar asked Sep 07 '10 09:09

nonozor


2 Answers

Is it possible ?

You can download an image. It will not be a "resource", though. Resources are packaged inside the APK and cannot be modified or added to at runtime.

like image 159
CommonsWare Avatar answered Oct 02 '22 20:10

CommonsWare


That's how I did it:

private class ImgDownload extends AsyncTask {
    private String requestUrl;
    private ImageView view;
    private Bitmap pic;

    private ImgDownload(String requestUrl, ImageView view) {
        this.requestUrl = requestUrl;
        this.view = view;
    }

    @Override
    protected Object doInBackground(Object... objects) {
        try {
            URL url = new URL(requestUrl);
            URLConnection conn = url.openConnection();
            pic = BitmapFactory.decodeStream(conn.getInputStream());
        } catch (Exception ex) {
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
        view.setImageBitmap(pic);
    }
}
like image 36
uncle Lem Avatar answered Oct 02 '22 20:10

uncle Lem