【问题标题】:Load bitmap from Internet and scale/sample it in android从 Internet 加载位图并在 android 中对其进行缩放/采样
【发布时间】:2015-07-02 12:26:53
【问题描述】:

我从互联网上下载了一张大图,并尝试使用以下方法对其进行缩放。问题是返回的位图为空。有没有不同的方法可以做到这一点,或者我在做不必要的事情。

public static Bitmap decodeBitmapFromInputStream(InputStream inputStream,
                                                 int reqWidth, int reqHeight) {
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(bis, null, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    options.inJustDecodeBounds = false;
    Bitmap b = BitmapFactory.decodeStream(bis, null, options);
    return b;
}


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 bitmap


【解决方案1】:

您必须复制 InputStream,因为您无法重新打开流。我有同样的问题,我就这样解决了:

 ... url conn ...
 InputStream in = null;
 InputStream in2 = null;
 in = urlConnection.getInputStream();
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 copy(in, out);
 in2 = new ByteArrayInputStream(out.toByteArray());
 bitmap = decodeSampledBitmapFromInputStream(in, in2, mPreferredWidth, mPreferredHeight);

....

public static Bitmap decodeSampledBitmapFromInputStream(InputStream in,
                                                        InputStream copyOfin, int reqWidth, int reqHeight) {
    BitmapFactory.Options options = getOptions(in, reqWidth, reqHeight);
    return BitmapFactory.decodeStream(copyOfin, null, options);
}

private static BitmapFactory.Options getOptions(InputStream data, int requiredWidth, int requiredHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(data, null, options);
    options.inSampleSize = getScale(options.outWidth, options.outHeight, requiredWidth, requiredHeight);
    options.inJustDecodeBounds = false;
    return options;
}

private static int getScale(int originalWidth, int originalHeight, final int requiredWidth, final int requiredHeight) {
    int scale = 1;
    if ((originalWidth > requiredWidth) || (originalHeight > requiredHeight)) {
        if (originalWidth < originalHeight)
            scale = Math.round((float) originalWidth / requiredWidth);
        else
            scale = Math.round((float) originalHeight / requiredHeight);
    }

    return scale;
}

public static int copy(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024 * 16];
    int count = 0;
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

希望对你有帮助!

【讨论】:

    【解决方案2】:
    public static Bitmap decodeBitmapFromInputStream(InputStream inputStream,
                                                     int reqWidth, int reqHeight) {
        Bitmap b = null;
           byte[] byteArr = new byte[0];
           byte[] buffer = new byte[1024];
           int len;
           int count = 0;
    
           try {
               while ((len = inputStream.read(buffer)) > -1) {
                   if (len != 0) {
                       if (count + len > byteArr.length) {
                           byte[] newbuf = new byte[(count + len) * 2];
                           System.arraycopy(byteArr, 0, newbuf, 0, count);
                           byteArr = newbuf;
                       }
    
                       System.arraycopy(buffer, 0, byteArr, count, len);
                       count += len;
                   }
               }
    
               final BitmapFactory.Options options = new BitmapFactory.Options();
               options.inJustDecodeBounds = true;
               BitmapFactory.decodeByteArray(byteArr, 0, count, options);
    
               options.inSampleSize = calculateInSampleSize(options, reqWidth,
                       reqHeight);
               options.inPurgeable = true;
               options.inInputShareable = true;
               options.inJustDecodeBounds = false;
               options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    
               //int[] pids = { android.os.Process.myPid() };
               //ActivityManager.MemoryInfo myMemInfo = mAM.getProcessMemoryInfo(pids)[0];
    
               return BitmapFactory.decodeByteArray(byteArr, 0, count, options);
    
           } catch (Exception e) {
               e.printStackTrace();
    
               return null;
           }
        return b;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      相关资源
      最近更新 更多