【问题标题】:Get large image as bitmap from url从 url 获取大图像作为位图
【发布时间】:2021-11-19 12:18:26
【问题描述】:
public Bitmap getBitmapFromURL(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection)
                    url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            Bitmap imageBitmap = BitmapFactory.decodeStream(inputStream);
            return imageBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

我使用此函数从 url 获取图像作为位图。对于大图像位图返回 null。我该如何解决这个问题?

【问题讨论】:

  • 大图是什么意思?一张大图大约 4-5MB。
  • @KhabyLame 图像大小 4961 × 3508 和大约 1.5MB。
  • 1.5MB 不算多
  • Bitmap.decodeStream() 如果位图对于所用设备中的可用内存而言变得太大,则确实返回 null。不是文件大小重要,而是分辨率。您必须先说明您想对文件做什么,然后我们才能建议您如何解决您的问题。
  • BitmapFactory 最多可以处理 100MB。我用我的佳能相机照片试过了。它大约 150MB,BitmapFactory 可以将其转换为位图。这取决于用户的连接

标签: android image bitmap


【解决方案1】:

代码已修改。看看这是否有效 如果位图为空,此代码将用另一个示例位图替换它。

  public Bitmap getBitmapFromURL(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection)
                    url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            Bitmap imageBitmap = BitmapFactory.decodeStream(inputStream);
            if(bitmap == null){
                imageBitmap = 
  BitmapFactory.decodeResource(context.getResources(),R.drawable.your_sample_image);
    
            return imageBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

【讨论】:

    【解决方案2】:

    试试这个:

     private Bitmap getBitmap(String image_link)
        {
            URL website;
    
            try {
                website = new URL(image_link);
                HttpURLConnection connection = (HttpURLConnection) website.openConnection();
                InputStream is = connection.getInputStream();
    
                if(req_width == 0)
                {
                    return BitmapFactory.decodeStream(is);
                }
    
    
                final BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(is, null, options);
                is.close();
    
    
                connection = (HttpURLConnection) website.openConnection();
                is = connection.getInputStream();
                options.inSampleSize = calculateInSampleSize(options, req_width, req_height);
                options.inJustDecodeBounds = false;
                return BitmapFactory.decodeStream(is, null , options);
    
            } catch (Exception  e) {
                return null;
            }
        }
    
        public static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
    
            if (height > reqHeight || width > reqWidth) {
    
                final int halfHeight = height / 2;
                final int halfWidth = width / 2;
    
                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) >= reqHeight
                        && (halfWidth / inSampleSize) >= reqWidth) {
                    inSampleSize *= 2;
                }
            }
    
            return inSampleSize;
        }
    

    【讨论】:

    • 我使用它,但不同版本的 Android 的相同设备不加载图像仅显示黑屏。有没有办法自动计算 reqHeight 和 reqWidth。
    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 2015-01-29
    • 2019-03-24
    • 1970-01-01
    • 2012-05-04
    • 2011-08-22
    • 1970-01-01
    相关资源
    最近更新 更多