来自URL Android的位图

时间:2016-07-12 09:59:18

标签: android url bitmap

我试图从URL获取png位图,但使用此代码时Bitmap始终为NULL:

    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);
    }
}

我在onCreate()中创建了一个DownloadImageTask:

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

我犯了一些错误吗?

5 个答案:

答案 0 :(得分:0)

这是一种简单的单行方式:

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

答案 1 :(得分:0)

如果您在浏览器中尝试使用http网址,则会看到它重定向到https。那是你的问题。 BitmapFactory.decodeStream不会执行此重定向,因此它返回null。

答案 2 :(得分:0)

您可以尝试下面的代码......

 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;
    }

答案 3 :(得分:0)

要从URL获取位图,请尝试以下操作:

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;
        }
    }

或者:

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;
}

答案 4 :(得分:0)

我建议使用Thead(异步)来避免出现异常错误:

        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();