我的应用程序出现以下问题: 当我用相机拍摄照片并按下发送到电子邮件按钮时,一切正常,图片保存在手机上,它正在加载到我的电子邮件中。但是,当我拍摄没有图像,我按下发送到电子邮件按钮应用程序崩溃!有人可以帮帮我吗?
电子邮件意图代码:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(foto));
相机意图代码:
FotoButton = (Button) findViewById(R.id.FotoButton);
FotoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startCamera();
}
});
}
private void startCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
foto = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Fehlerbild.jpg");
Uri image = Uri.fromFile(foto);
intent.putExtra(MediaStore.EXTRA_OUTPUT, image);
startActivityForResult(intent,TAKE_FOTO);
崩溃的代码:
04-02 11:57:30.229 21042-21042/de.cmoreno.hcsedv_service E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.cmoreno.hcsedv_service, PID: 21042
java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:448)
at de.cmoreno.hcsedv_service.Main2Activity$1.onClick(Main2Activity.java:174)
at android.view.View.performClick(View.java:5242)
at android.widget.TextView.performClick(TextView.java:10573)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
答案 0 :(得分:0)
如果您不包含任何照片,foto
为null
:
foto = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Fehlerbild.jpg");
在这种情况下尽量不要包括它。
编辑1:
基于评论:
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(foto)); // this is causing problem.
将其更改为:
if(foto!=null){
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(foto));
}