Android:从流中解码位图

时间:2013-07-25 08:21:58

标签: android bitmap inputstream

我有这个代码来从用户外部存储器获取位图

    /**
 * 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);
}

它适用于大多数支持,但有些我有这个警告

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)

这是关于这部分的

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

我真的不明白不能被执行。

感谢您的帮助

1 个答案:

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

使用上面的静态方法从外部存储中获取位图

正确提供filePath .....