这是我的代码显示图片:
//从fileName中取消图像
public Bitmap decodeSampledBitmapFromResource(String fileName,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// BitmapFactory.decodeResource(res, resId, options);
BitmapFactory.decodeFile(fileName, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(fileName, options);
}
//将图像显示到ImageView
private void addFiletoList(String fileName) {
DTO_FileInfo dt = new DTO_FileInfo();
dt.FileName = fileName;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
targetImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
Bitmap bmp = Bitmap.createBitmap(decodeSampledBitmapFromResource(
dt.FileName, 50, 50));
targetImage.setImageBitmap(bmp);
}
但它出现错误:
java.lang.NullPointerException
at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
at com.noen.maihue.camerapro.CameraApp.addFiletoList(CameraApp.java:1141)
at com.noen.maihue.camerapro.CameraApp.access$2(CameraApp.java:1123)
at com.noen.maihue.camerapro.CameraApp$SavePhotoTask.onPostExecute(CameraApp.java:1290)
at com.noen.maihue.camerapro.CameraApp$SavePhotoTask.onPostExecute(CameraApp.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
仅在某些手机上发生:Panasonic P55 Novo,IX1AT8 Android 4.4
为什么会出现错误java.lang.NullPointerException Bitmap.java:620?
答案 0 :(得分:0)
此代码返回null
:
decodeSampledBitmapFromResource(dt.FileName, 50, 50)
导致Bitmap.decode(Bitmap)
失败并抛出NullPointerException
的原因。
其中一个原因可能是内存问题(没有足够的内存来读取文件和解码)或者文件(dt.FileName
)无法找到或无法访问。