Quarillion
Quarillion

Reputation: 65

Bitmap from URL Android

I'm trying to get a png Bitmap from URL but the Bitmap is always NULL in with this code:

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;
    Activity activity;

    public DownloadImageTask(ImageView bmImage, Activity activity) {
        this.bmImage = bmImage;
        this.activity = activity;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Log.i("LEGGERE", urldisplay);
        Bitmap mIcon11 = null;
        try {
            URL url = new URL(urldisplay);
            mIcon11 = BitmapFactory.decodeStream(url.openConnection().getInputStream());

            if (null != mIcon11)
                Log.i("BITMAP", "ISONOTNULL");
            else
                Log.i("BITMAP", "ISNULL");
        } catch (Exception e) {
            Log.e("Error", "PORCA VACCA");

        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

And I create a DownloadImageTask in onCreate():

new DownloadImageTask((ImageView) findViewById(R.id.provaaa),this)
            .execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");

Do I make some mistakes?

Upvotes: 0

Views: 6322

Answers (5)

Renaud
Renaud

Reputation: 11

I suggest to use a Thead (async) to avoid exception errors :

        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {
                Bitmap thumb;
                // Ur URL
                String link = value.toString();
                try {
                    URL url = new URL(link);
                    thumb = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                   // UI component
                    imageView.setImageBitmap(thumb);

                } catch (Exception e) {
                    Log.e("error message", Objects.requireNonNull(e.getMessage()));
                }
            }
        });
        thread.start();

Upvotes: 1

SGX
SGX

Reputation: 3353

To get a bitmap from URL, try this:

public Bitmap getBitmapFromURL(String src) {
        try {
            java.net.URL url = new java.net.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;
        }
    }

Or:

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}

Upvotes: 0

greenapps
greenapps

Reputation: 11214

If you try the http url in a browser you see that it redirects to a https. Thats your problem. BitmapFactory.decodeStream will not do this redirection so it returns null.

Upvotes: 0

Iris Louis
Iris Louis

Reputation: 297

You can try code below...

 public static Bitmap loadBitmap(String url) {
        Bitmap bitmap = null;
        InputStream in = null;
        BufferedOutputStream out = null;
        int IO_BUFFER_SIZE = 4 * 1024;
        try {
            URI uri = new URI(url);
            url = uri.toASCIIString();
            in = new BufferedInputStream(new URL(url).openStream(),
                    IO_BUFFER_SIZE);
            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
            int bytesRead;
            byte[] buffer = new byte[IO_BUFFER_SIZE];
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            out.flush();
            final byte[] data = dataStream.toByteArray();
            BitmapFactory.Options options = new BitmapFactory.Options();
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
                    options);
        } catch (IOException e) {
            return null;
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

Upvotes: 0

Nitesh Pareek
Nitesh Pareek

Reputation: 362

This is a simple one line way to do it:

 URL url = new URL("http://....");
    Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());

Upvotes: 1

Related Questions