解码大小后如何从输入流中获取位图



我想从输入流中获取位图,然后调整它的大小。但是我得到空指针异常。

我尝试输入流.reset(),但不起作用。

有人可以帮忙吗?

 BufferedInputStream my_is = new BufferedInputStream(is);
                Bitmap    bit           = null;
                final BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(my_is, null, options);
                // Calculate inSampleSize
                options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
                // Decode bitmap with inSampleSize set
                options.inJustDecodeBounds = false;
                try {
                    my_is.reset();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                bit     = BitmapFactory.decodeStream(my_is, null, options);
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                        Locale.getDefault()).format(new Date());
                FileOutputStream fos = null;
                try {
                    fos = getActivity().openFileOutput(timeStamp + ".png", Context.MODE_PRIVATE);
                    bit.compress(Bitmap.CompressFormat.PNG,90,fos);
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                crud.update_photo_path(String.valueOf(position), timeStamp);
                return bit;

我使用了这个实用程序:

public static Bitmap decodeSampledBitmapFromResource(String uri,
        int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(uri, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(uri, options);
}
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) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

最新更新