2017-05-11 1 views
0

Je construis une petite application Android qui permet aux gens de donner de la nourriture par courrier électronique. J'ai des problèmes avec prendre une photo et l'envoyer en pièce jointe. Je sais que je suis en train d'initier deux activités différentes simultanément et si c'est la raison pour laquelle l'application plante, pourrait-il y avoir un moyen de définir le type d'intention pour lui permettre d'utiliser différents types de fichiers ou de contenus)? Ou d'autres suggestions?Problèmes liés à la prise de photo et à l'envoi via un client de messagerie (application Android)

Java:

package com.example.android.foodshare; 

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Toast; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 


public class Donation extends AppCompatActivity implements View.OnClickListener { 

Button submit, open_cam; 
EditText company_name; 
EditText phone_number; 
EditText location; 
EditText foodType; 
EditText quantity; 
ImageView the_food; 
File pic; 

String companyNameString; 
String phoneNumberString; 
String locationString; 
String foodTypeString; 
String quantityString; 

String emailTo = "[email protected]"; 
String emailContent; 
String emailSubject; 

static final int REQUEST_IMAGE_CAPTURE = 0; 
Bitmap imageBitmap; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.donation); 

    submit = (Button) findViewById(R.id.bSubmit); 
    open_cam = (Button) findViewById(R.id.bCam); 
    the_food = (ImageView) findViewById(R.id.ivFood); 
    company_name = (EditText) findViewById(R.id.etCompName); 
    phone_number = (EditText) findViewById(R.id.etPhone); 
    location = (EditText) findViewById(R.id.etLocation); 
    foodType = (EditText) findViewById(R.id.etFoodType); 
    quantity = (EditText) findViewById(R.id.etQuantity); 
    submit.setOnClickListener(this); 
    open_cam.setOnClickListener(this); 
} 
@Override 
public void onClick(View v) { 
    switch (v.getId()) { 

     case R.id.bCam: 

      //more pic handling 
       Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
       } 
      break; 

     case R.id.bSubmit: 
      companyNameString = company_name.getText().toString(); 
      phoneNumberString = phone_number.getText().toString(); 
      locationString = location.getText().toString(); 
      foodTypeString = foodType.getText().toString(); 
      quantityString = quantity.getText().toString(); 

      emailContent = "You have received a Food Pickup Request. Please find details below:\n\n" + 
        "Name of Organization (or Individual): " + companyNameString + "\n" + 
        "Telephone Number: " + phoneNumberString + "\n" + 
        "Location: " + locationString + "\n" + 
        "Available Food Type: " + foodTypeString + "\n" + 
        "Quantity Available: " + quantityString; 

      emailSubject = "New Food Pickup Request From " + companyNameString; 

      Intent emailIntent = new Intent(Intent.ACTION_SEND); 
      emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailTo}); 
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject); 
      emailIntent.putExtra(Intent.EXTRA_TEXT, emailContent); 
      emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic)); 

      // new line added 
      emailIntent.setType("image/png"); 
      startActivity(Intent.createChooser(emailIntent,"Share you on the jobing")); 

      //need this to prompt email client only 
      emailIntent.setType("message/rfc822"); 

      try { 
       startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
      } catch (android.content.ActivityNotFoundException ex) { 
       Toast.makeText(Donation.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
      } 
      break; 


    } // end of switch statement 

    }; 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     imageBitmap = (Bitmap) extras.get("data"); 
     the_food.setImageBitmap(imageBitmap); 


     try { 
      File root = Environment.getExternalStorageDirectory(); 
      if (root.canWrite()){ 
       pic = new File(root, "pic.png"); 
       FileOutputStream out = new FileOutputStream(pic); 
       imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 
       out.flush(); 
       out.close(); 
      } 
     } catch (IOException e) { 
      Log.e("BROKEN", "Could not write file " + e.getMessage()); 
     } 



    } 
} 



} 

Trace de la pile:

05-11 19: 56: 00,415 2787-2787/com.example.android.foodshare E/AndroidRuntime: EXCEPTION FATAL: principal processus: com.example.android.foodshare, PID: 2787 java.lang.NullPointerException: fichier à android.net.Uri.fromFile (Uri.java:452) à com.example.android.foodshare.Donation.onClick (Donation .java: 94) à android.view.View.performClick (View.java:5637) à android.view.View $ PerformClick.run (View.java:22429) à android.os.Handler.handleCallback (Handler.java:751) à android.os.Handler.dispatchMessage (Handler.java:95) à android.os.Looper.loop (Looper.java:154) à android.app.ActivityThread.main (ActivityThread.java:6119) à java.lang.reflect.Method.invoke (méthode native) à com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:886) à com.android.internal.os.ZygoteInit.main (ZygoteInit .java: 776)

+0

"si c'est le problème pourquoi l'application se bloque" - Si l'application plante, utilisez LogCat pour examiner la trace de la pile Java: https://stackoverflow.com/questions/23353173/unheureusement-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

bon point. J'ai posté la trace de la pile sachant que les problèmes étaient liés au composant "fichier". Toujours besoin d'aide pour le débogage :) – Kalid

Répondre

0
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic)); 

pic est null. Vous affectez uniquement une valeur à pic dans onActivityResult() de votre ACTION_IMAGE_CAPTUREIntent. Si vous avez besoin de pic pour votre e-mail, vous devez désactiver le bouton e-mail jusqu'à ce que vous ayez votre pic.

aussi:

  • Ne pas appeler startActivity() deux fois de suite, comme vous le faites dans le bSubmit cas

  • Votre deuxième startActivity() est invalide, comme vous prétendez que EXTRA_STREAM points du texte dans message/rfc822 format, et il ne le fait pas