2016-04-02 11 views
0

Ich habe folgendes Problem mit meiner App: Wenn ich ein Foto mit der Kamera mache und den SEND TO EMAIL Button drücke funktioniert alles gut, das Bild wird gespeichert am Telefon und es wird geladen, um meine E-Mail-Absicht. ABER wenn ich kein Bild mache und den SEND TO EMAIL Button drücke, stürzt die App ab! Könnte mir jemand helfen?Android App: Die App stürzt ab wenn ich kein Foto mit der Kamera mache

Die E-Mail-Intent-Code:

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

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

Der 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) 
+0

Post Crash-Protokoll ... –

+1

Zeigen Sie uns Ihre activityforresult. –

+0

Sorry, ich bin neu bei Android und ich habe keine Aktivitäten für das Ergebnis. – Chris

Antwort

0

Wenn Sie ein Foto ohne sind, foto ist null :

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

Versuchen Sie, dies in diesem Fall nicht einzubeziehen.

Edit 1:

auf Kommentare Basierend:

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

ändern sie dies mag:

if(foto!=null){ 
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(foto)); 
} 
+0

Ausnahme bei: 'Main2Activity.java: 174' –

+0

Könntest du mir bitte ein Beispiel mit meinem Code zeigen? – Chris

+0

Ausnahme bei: Main2Activity.java:174 = emailIntent.putExtra (Intent.EXTRA_STREAM, Uri.fromFile (foto)); – Chris

Verwandte Themen