我正在尝试执行以下任务:
- 拍照
- 将其存储在 sd 卡中并将路径保存在字符串中。
代码:
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);
它返回空值。谁能解释发生了什么//?