Android:从流解码位图
问题描述:
我有这段代码可以从用户外部存储中获取位图
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)
这是关于这个部分
try {
bis.reset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我真的不知道无法执行.
And I don't realy understand can't be executed.
感谢您的帮助
答
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
正确提供filePath .....
Give the filePath ..... correctly