繁体   English   中英

来自 URL Android 的位图

[英]Bitmap from URL Android

我正在尝试从 URL 获取 png 位图,但此代码中的位图始终为 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");

我会犯一些错误吗?

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

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

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

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

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

你可以试试下面的代码...

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM