从Internet加载位图并在android中扩展/采样

时间:2015-07-02 12:26:54

标签: android bitmap

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

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

2 个答案:

答案 0 :(得分: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;
}

希望它有所帮助!

答案 1 :(得分:0)

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