我正在尝试执行以下任务:
代码:
public synchronized void onActivityResult(final int requestCode, int resultCode, final Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
if (requestCode == RESULT_CAMERA_SELECT)
{
try
{
photo = null;
saveImage();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
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) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public void saveImage() throws IOException
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
FileInputStream is2 = new FileInputStream(new File(myReceipt.filename));
BitmapFactory.decodeStream(is2 ,null, options);
// Here is2 get file with width of pic as 2000*1500 etc
options.inSampleSize = calculateInSampleSize(options, 100, 100);
options.inJustDecodeBounds = false;
// PROBLEM EXISTS AT THIS POINT. imageBitmap is returned as null.....
Bitmap imageBitmap = BitmapFactory.decodeStream(is2 ,null, options);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 100, 100, false);
try{
photo = this.createFile(fileName, ".jpg");
thumbFileName = photo.getAbsolutePath();
Uri uri = Uri.fromFile(photo);
try {
FileOutputStream out = new FileOutputStream(photo);
imageBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
is2.close();
photo = null;
imageBitmap = null;
imageView.setImageURI(uri);
}catch(Exception e)
{
displayAlert("Can't create file to take picture!","SD Card Error");
}
}
根据代码,我正在拍摄一张图片,然后制作一个拇指图像,在图像视图中显示它,并存储此thumbImage。但错误发生在
Bitmap imageBitmap = BitmapFactory.decodeStream(is2 ,null, options);
它返回null。任何人都可以解释发生了什么//?
答案 0 :(得分:4)
您只能使用InputStream
一次!
致电时
BitmapFactory.decodeStream(is2 ,null, options);
它将“消耗”is2
。获得
Bitmap imageBitmap = BitmapFactory.decodeStream(is2 ,null, options);
工作,您必须为它创建一个新的InputStream
,并在那里明确使用它。
答案 1 :(得分:0)
试试这个。
ContentResolver cr = mContext.getContentResolver();
用cr.openInputStream(uri)替换is2;
Bitmap imageBitmap = BitmapFactory.decodeStream(cr.openInputStream(uri),null,options);
这对我来说很好。
答案 2 :(得分:-1)
NullPointerException
。因此,请查看您的图片网址或位置是否正确,因为有时JRE无法找到图片或使用图片并提供NullPointerException
。它总是发生在我身上。