问题描述
下面是我的onActivityResult()
的全部代码 @Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath);
ExifInterface exif = null;
try {
exif = new ExifInterface(imageFilePath);
} catch (IOException e) {
e.printstacktrace();
}
int exifOrientation;
int exifDegree;
if (exif != null) {
exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_norMAL);
exifDegree = exifOrientationToDegress(exifOrientation);
}
else {
exifDegree = 0;
}
String result = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HHmmss",Locale.getDefault() );
Date curDate = new Date(System.currentTimeMillis());
String filename = formatter.format(curDate);
String strFolderName = Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES) + File.separator + "HENDRIK" + File.separator;
File file = new File(strFolderName);
if( !file.exists() )
file.mkdirs();
File f = new File(strFolderName + "/" + filename + ".png");
result = f.getPath();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printstacktrace();
result = "Save Error fOut";
}
rotate(bitmap,exifDegree).compress(Bitmap.CompressFormat.PNG,70,fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printstacktrace();
}
try {
fOut.close();
mMediaScanner.mediaScanning(strFolderName + "/" + filename + ".png");
} catch (IOException e) {
e.printstacktrace();
result = "File close Error";
}
((ImageView) findViewById(R.id.iv_result)).setimageBitmap(rotate(bitmap,exifDegree));
}
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printstacktrace();
result = "Save Error fOut";
}
rotate(bitmap,fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printstacktrace();
}
似乎 fOut 为空,这就是错误发生的原因,但我不明白为什么 fOut 为空
下面是logcat 控制台
2021-07-13 18:55:35.908 8337-8337/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2021-07-13 18:55:35.909 8337-8337/? E/Zygote: accessInfo : 1
2021-07-13 18:55:35.924 8337-8337/? E/e.cameraexampl: UnkNown bits set in runtime_flags: 0x8000
2021-07-13 18:55:43.017 8337-8337/com.example.cameraexample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cameraexample,PID: 8337
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,request=672,result=-1,data=null} to activity {com.example.cameraexample/com.example.cameraexample.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:5360)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5401)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8154)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.compress(Bitmap.java:1685)
at com.example.cameraexample.MainActivity.onActivityResult(MainActivity.java:144)
at android.app.Activity.dispatchActivityResult(Activity.java:8300)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5353)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5401)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8154)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)