0

i have following Problem with my app: When i take a foto with the camera and press the SEND TO EMAIL Button it all works fine, the picture is saved on the phone and it is loading up to my email Intent. BUT when i take NO image and i press the SEND TO EMAIL Button the app crashes ! Could someone Help me out ??

The Email Intent Code:

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(foto));

The Camera Intent Code:

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);

The Crashed Code:

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)
luchaninov
  • 6,792
  • 6
  • 60
  • 75
Chris
  • 3
  • 3

1 Answers1

0

When you are not including any photo, foto is null:

foto = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Fehlerbild.jpg");

Try not to include this in that case.

Edit 1:

Based on comments:

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(foto)); // this is causing problem.

change it to like this:

if(foto!=null){
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(foto));
}
Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
  • Exception at : `Main2Activity.java:174` – Rohit Arya Apr 02 '16 at 10:02
  • could you pleaswe show me an example with my code ?? – Chris Apr 02 '16 at 10:04
  • Exception at : Main2Activity.java:174 = emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(foto)); – Chris Apr 02 '16 at 10:06
  • Here `foto` is null because you haven't sent any photo. – Rohit Arya Apr 02 '16 at 10:10
  • OK It Works thanks a lot !! – Chris Apr 02 '16 at 12:51
  • Hi @Chris, if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. BTW there is no obligation to do this. – Rohit Arya Apr 02 '16 at 13:06