我正在创建一个应用程序,允许用户捕获和成像,然后在拼图上使用该图像。我可以成功使用相机,但在图像捕获后,当我应该被带到拼图屏幕,从本地存储加载图像时,我得到一个FNF异常。 (我在应用程序中有一个部分,显示用户可以用于拼图的图像,并且新捕获的图像显示在那里 - 重新启动应用程序后,因为它已经崩溃)。
我的代码如下
public static Bitmap decodeSampledBitmapFromPath(String filepath, int reqWidth, int reqHeight) throws FileNotFoundException {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return Bitmap.createScaledBitmap(BitmapFactory.decodeFile(filepath),
reqWidth, reqHeight, false);
}
在return
线上抛出异常。请帮我解决这个问题。谢谢。编辑:包裹试试围绕return
线,现在Logcat显示
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at apps.mine.puzzle.Board.countTileSize(Board.java:60)
at apps.mine.puzzle.PlayPuzzleActivity.onCreate(PlayPuzzleActivity.java:138)
我有类似的问题,这将解决它:
if(BitmapFactory.decodeFile(Image[position])!=null)
{
Bitmap bitmap=Bitmap.createScaledBitmap(BitmapFactory.decodeFile(Image[position]), 32, 32, true);
imageView.setImageBitmap(bitmap);
}
else
{
Log.d("TAG", "unable to decode");
}
问题的主要原因是decodeResource
因为以下原因之一而返回null:
Updte
如果你不想像@Zoe指出的那样对文件进行两次解码,你可以修改decodeResource
的代码,这样它就可以进行无效检查,而不必两次,如下所示:
public class BitmapScalingHelper
{
public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeight)
{
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth,
dstHeight);
options = new Options();
Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options);
if(unscaledBitmap == null)
{
Log.e("ERR","Failed to decode resource" + resId + " " + res.toString());
return null;
}
return unscaledBitmap;
}
}