2017-05-10 7 views
0

私は、電子メールクライアントを通じて人々が食品を寄付できる小さなAndroidアプリを構築しています。写真を撮ってサムネイルを表示した後、私が送信を押すと、Gmailアプリ(電子メールクライアント)がクラッシュします。基本的には、今撮影した画像から画像添付ファイルとともにメールでユーザー入力情報を送信したいと考えています。写真を撮って電子メールクライアント(アンドロイドアプリ)で添付ファイルとして送信することに直面する問題

私はイメージファイルの添付領域の周りに何か間違っていると知っています。 ヘルプ!

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:weightSum="1"> 

<TextView 
    android:id="@+id/textView7" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Name of Company/Individual:" /> 

<EditText 
    android:id="@+id/etCompName" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="textPersonName" 
    android:textAllCaps="true" 
    android:hint="Ex: Chicken Hut, Kaldi's Coffee, etc" /> 

<TextView 
    android:id="@+id/textView8" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Telephone Number (required)" /> 

<EditText 
    android:id="@+id/etPhone" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:hint="Ex: 0922092161" 
    android:inputType="phone" /> 

<TextView 
    android:id="@+id/textView9" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Describe your Location:" /> 

<EditText 
    android:id="@+id/etLocation" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="textPersonName" 
    android:textAllCaps="true" 
    android:hint="Briefly describe where you are located..." /> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Food Type:" /> 

<EditText 
    android:id="@+id/etFoodType" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="textPersonName" 
    android:textAllCaps="true" 
    android:hint="Ex: Local Food, Sandwiches, etc." /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Estimated Quantity (in grams):" /> 

<EditText 
    android:id="@+id/etQuantity" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="textPersonName" 
    android:textAllCaps="true" 
    android:hint="Ex: 1kg, 50g, etc." /> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Snap a Picture of Food (if available)" /> 

<ImageView 
    android:id="@+id/ivFood" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/ic_launcher" 
    android:layout_weight="0.9" /> 

<Button 
    android:id="@+id/bCam" 
    style="@style/Widget.AppCompat.Button.Borderless.Colored" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:elevation="0dp" 
    android:background="#ee9d3e" 
    android:text="Open Camera" 
    android:textColor="#ffffff" /> 

<Button 
    android:id="@+id/bSubmit" 
    android:background="#cecbc8" 
    android:textColor="#ffffff" 
    style="@android:style/Widget.DeviceDefault.Button.Borderless" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Submit" /> 

    </LinearLayout> 

JAVA:

package com.example.android.foodshare; 

    import android.content.Intent; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.os.Environment; 
    import android.provider.MediaStore; 
    import android.support.v7.app.AppCompatActivity; 
    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.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; 

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

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

static final int REQUEST_IMAGE_CAPTURE = 1; 
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: 


       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 further 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; // maybe add from which company 

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

      //need this to prompts 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); 
    } 
} 



    } 

答えて

0

the documentation for EXTRA_STREAMを引用:

コンテンツを:URIを供給するACTION_SENDとともに使用、インテントに関連付けられたデータのストリームを保持しますデータが送信されています

Uricontentスキームを指定していません。代わりに、Bitmapを提供しています。 BitmapUriにキャストしようとしているため、Gmailがクラッシュしている可能性があります。

関連する問題