NullPointerException
方法中的某些手机获取OnPictureTaken
。我正在调用像这样的takePicture方法。
mCamera.takePicture(shutterCallback, null, mPicture);
我的OnPictureTaken
方法
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = null;
if(flashOff) {
resetCamOnFlashOFF();
}
else {
resetCamOnFlashOn();
}
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File dir = new File(sdDir.getAbsolutePath() + "/Camera2/");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fileName = "/IMG_" + timeStamp + ".jpeg";
boolean mkDir = dir.mkdirs();
Log.d("Saving Photo","Created the directory" +mkDir);
ExifInterface exif;
try {
exif = new ExifInterface(fileName);
exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orientation);
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
int reqHeight = options.outHeight;
int reqWidth = options.outWidth;
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
}catch (OutOfMemoryError e){
e.printStackTrace();
System.gc();
}
Matrix matrix = new Matrix();
switch(orientation){
case ExifInterface.ORIENTATION_NORMAL : matrix.postRotate(0);
break;
case ExifInterface.ORIENTATION_ROTATE_90 : matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180 : matrix.postRotate(180);
break;
default: matrix.postRotate(270);
break;
}
assert bitmap != null;
bitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
File outFile = new File(dir, fileName);
try {
FileOutputStream outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 95, outStream);
bitmap.recycle();
outStream.write(data);
outStream.flush();
outStream.close();
refreshGallery(outFile);
} catch (IOException e) {
e.printStackTrace();
}
mPreview.safeToTakePicture = true;
}
};
仅在少数手机上我收到错误NullPointerException
错误详情:
NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
com.myDomain.camera2.r.onPictureTaken in SCamera.java:482
android.hardware.Camera$EventHandler.handleMessage in Camera.java:1142
android.os.Handler.dispatchMessage in Handler.java:102
android.os.Looper.loop in Looper.java:145
android.app.ActivityThread.main in ActivityThread.java:6141
java.lang.reflect.Method.invoke in Method.java:-2
java.lang.reflect.Method.invoke in Method.java:372
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run in ZygoteInit.java:1399
com.android.internal.os.ZygoteInit.main in ZygoteInit.java:1194
答案 0 :(得分:0)
删除代码
options.inJustDecodeBounds = true;
小心OOM。 或试试这个:
options.inJustDecodeBounds = true;
options.inSampleSize = 5;
options.inJustDecodeBounds = false;