【问题标题】:Load large bitmap from url and resize it从 url 加载大位图并调整其大小
【发布时间】:2013-12-19 21:51:27
【问题描述】:

我想从 url 加载巨大的图像并在我的设备上显示。我为此阅读了this 的帖子。

在该示例中,他们使用本机可绘制对象,但我想从服务器获取图像。

我正在使用此代码

 private void getBitmap(ImageView imageView,final String url){

        mBitmapOptions = new BitmapFactory.Options();
        mBitmapOptions.inJustDecodeBounds = true;
        URL mUrl = null;
        InputStream is= null;
        try {
            mUrl = new URL(url);
            is = mUrl.openStream();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        BitmapFactory.decodeStream(is,null,mBitmapOptions);

        mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth,
                mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
        mBitmapOptions.inJustDecodeBounds = false;
        mBitmapOptions.inBitmap = mCurrentBitmap;
        mBitmapOptions.inSampleSize = 1;
        BitmapFactory.decodeStream(is,null,mBitmapOptions);
        imageView.setImageBitmap(mCurrentBitmap);
//        Bitmap croppedBitmap=Bitmap.createBitmap(Bitmap.createBitmap(mCurrentBitmap, 0, mCurrentBitmap.getHeight()/2, mCurrentBitmap.getWidth(), mCurrentBitmap.getHeight()/2));
//        imageView.setImageBitmap(croppedBitmap);
    }

我想在这里从 url 获取图像并调整它的大小,但我有一个例外。

java.lang.IllegalArgumentException: width and height must be > 0

我在这里做错了什么?或者你能建议我更好的答案吗?

【问题讨论】:

  • 谢谢@Damian,但毕加索会自动处理大图像加载,还是我应该自己处理?
  • 您评论的 BitmapFactory.decodeStream(is,null,mBitmapOptions); 是在这里实际填充位图选项中的宽度和高度。您最好先下载文件,然后对文件执行此操作。
  • @njzk2 有几张图片的尺寸很大,我认为在 perferm 操作后下载所有图片并不是最好的方法。
  • 无论如何你都必须下载它们,你知道的。因为如果不下载它们,你就无法知道它们的大小或 b/ 显示它们
  • @pmb 你得到异常是因为// BitmapFactory.decodeStream(is,null,mBitmapOptions); 在你尝试解码时没有设置 mBitmapOptions。查看我之前的回答,尝试解决您的完整解码问题。

标签: android bitmap android-imageview android-image bitmapimage


【解决方案1】:

我会用毕加索,那么你想要实现的就这么简单:

Picasso.with(context).load(url).resize(50, 50).into(imageView)

http://square.github.io/picasso/

【讨论】:

    【解决方案2】:

    你得到异常是因为:

    // BitmapFactory.decodeStream(is,null,mBitmapOptions);
    

    尝试调用时没有设置 mBitmapOptions:

    mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth, mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
    

    查看此问题的答案以进一步优化您的代码:Getting the size of an image inputstream

    【讨论】:

      【解决方案3】:

      我根据http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 教程开发了一个解决方案。这是我的代码。棘手的部分包括使用字节数组来存储图像数据(因为不幸的是markSupported() 不适用于connection.getInputStream())。

          public static Bitmap getBitmapFromURL(final String imageUrl, final Options options, int reqWidth, int reqHeight) {
          try {
              URL url = new URL(imageUrl);
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection.setDoInput(true);
              connection.connect();
              final InputStream input = connection.getInputStream();
              // using byte array to prevent open 2 times a stream
              final BufferedInputStream bis = new BufferedInputStream(input, 4*1024);
              ByteArrayBuffer baf = new ByteArrayBuffer(50);
              int current = 0;
              while ((current = bis.read()) != -1) {
                  baf.append((byte)current);
              }
              bis.close();
              byte[] imageData = baf.toByteArray();
              if(options != null){
                  // First decode with inJustDecodeBounds=true to check dimensions
                  final BitmapFactory.Options optionsSize = new BitmapFactory.Options();
                  optionsSize.inJustDecodeBounds = true;
      
      
                  BitmapFactory.decodeByteArray(imageData, 0, imageData.length, optionsSize);
      
                  // Calculate inSampleSize
                  options.inSampleSize = calculateInSampleSize(optionsSize, reqWidth, reqHeight);
      
                  // Decode bitmap with inSampleSize set
                  optionsSize.inJustDecodeBounds = false;
      
              }
              Bitmap myBitmap = null;
              if(options == null){
                  myBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
              }
              else{
                  myBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
              }
              // close the stream;
              input.close();
              return myBitmap;
          } catch (Exception e) {
              return null;
          }
      }
      
      private 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;
      }
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2012-10-02
        • 2010-12-29
        • 1970-01-01
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多