Android: decode bitmap from stream

跟風遠走 提交于 2019-12-25 04:11:43

问题


I have this code to get bitmaps from the user external storage

    /**
 * Get bitmap from input stream
 * @param is
 * @param reqWidth
 * @param reqHeight
 * @return Bitmap
 */
public static Bitmap decodeSampleBitmapFromStream(InputStream is, int reqWidth,
        int reqHeight) {

    BufferedInputStream bis = new BufferedInputStream(is);

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(bis, null, options);

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

    try {
        bis.reset();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(bis, null, options);
}

It works fine on most of supports but on some I have this warning

07-23 21:06:32.355: D/PowerManagerService(2687): onSensorChanged: light value: 10
07-23 21:06:32.510: W/System.err(26270): java.io.IOException: Mark has been invalidated.
07-23 21:06:32.510: W/System.err(26270):          at java.io.BufferedInputStream.reset(BufferedInputStream.java:371)
07-23 21:06:32.510: W/System.err(26270):          at ant.fileExplorer.FileExplorerAdapter.decodeSampleBitmapFromStream(FileExplorerAdapter.java:168)

This is about this part

try {
        bis.reset();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

And I don't realy understand can't be executed.

Thanks for your help


回答1:


public static Bitmap decodeSampleBitmapFromFile(String filePath, int reqWidth,
        int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath,options);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath,options);
}

use the above static method to get bitmap from the external storage

Give the filePath ..... correctly



来源:https://stackoverflow.com/questions/17852703/android-decode-bitmap-from-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!