2017-05-11 46 views
0

我正在构建一个小型Android应用程序,让人们通过电子邮件捐赠食物。我正在拍照并将其作为电子邮件附件发送。我知道我正在同时启动两项不同的活动,如果这是应用程序崩溃的问题,是否可以设置意图类型以便允许它使用不同类型的文件或内容(本例中的图片和文本)?还是有其他建议?拍摄照片并通过电子邮件客户端发送问题(Android应用程序)

爪哇:

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



    } 
} 



} 

堆栈跟踪:

05-11 19:56:00.415 2787年至2787年/ com.example.android.foodshare E/AndroidRuntime:致命异常:主 过程: com.example.android.foodshare,PID:2787 java.lang.NullPointerException:file at android.net.Uri.fromFile(Uri.java:452) at com.example.android.foodshare.Donation.onClick(Donation .java:94) at android.view.View.performClick(View.java:5637) at android.view.View $ PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:776)

+0

“如果是这样的问题,为什么应用程序崩溃” - 如果应用程序崩溃,请使用LogCat检查Java堆栈跟踪:https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

好点。我已经发布了堆栈跟踪,知道“文件”组件面临的问题。仍然需要帮助调试:) – Kalid

回答

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

picnull。您只需在ACTION_IMAGE_CAPTUREIntentonActivityResult()中为pic指定一个值。如果您要为您的电子邮件要求pic,则应该禁用电子邮件按钮,直到您拥有pic

另外:

  • 不要连续拨打startActivity()两次,因为你是在bSubmit情况下做

  • 你的第二个startActivity()是无效的,因为你是声称EXTRA_STREAM指向文本message/rfc822格式,它不

相关问题