2014-09-29 42 views
0

我有一个按钮,拍摄照片,在存储的ImageView的缩略图,当用户点击另一个按钮,它需要的是图像附加到电子邮件。附上相机电子邮件图像 - 机器人

香港专业教育学院遵循“拍摄照片只需”从Android网站的方向,但不包含附件。有人可以看到我出错的地方,我需要做些什么来解决它?

我对活动结果代码:

public void onActivityResult(int requestcode, int resultcode, Intent data) { 
     if (requestcode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
      if (resultcode == RESULT_OK) { 
       Bundle bundle = new Bundle(); 
       bundle = data.getExtras(); 
       Bitmap BMB; 
       BMB = (Bitmap) bundle.get("data"); 
       Hoon_Image.setImageBitmap(BMB); 
       try { 
        createImageFile(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       mDrawable = Hoon_Image.getDrawable(); 
       mBitmap = ((BitmapDrawable) mDrawable).getBitmap(); 
      } 
     } 
    } 

private File createImageFile() throws IOException { 
     // Create an image file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File storageDir = Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES); 
     File image = File.createTempFile(
       imageFileName, /* prefix */ 
       ".jpg",   /* suffix */ 
       storageDir  /* directory */ 
     ); 

     // Save a file: path for use with ACTION_VIEW intents 
     mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
     return image; 
    } 

我的电子邮件的意图:

if (Witness_response == "Yes"){ 
        Intent emailIntent = new Intent(Intent.ACTION_SEND); 
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Dob in a Hoon Report (Y)"); 
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Hoon report has been recieved " + emailBody); 
        emailIntent.putExtra(Intent.EXTRA_STREAM, mCurrentPhotoPath); 
        emailIntent.setType("message/rfc822"); 
        startActivity(Intent.createChooser(emailIntent, "Choose email client...")); 
       } else if (Witness_response == "No"){ 
        Intent emailIntent = new Intent(Intent.ACTION_SEND); 
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Dob in a Hoon Report (N)"); 
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Hoon report has been recieved " + emailBody); 
        emailIntent.putExtra(Intent.EXTRA_STREAM, mCurrentPhotoPath); 
        emailIntent.setType("message/rfc822"); 
        startActivity(Intent.createChooser(emailIntent, "Choose email client...")); 
       } 

回答

1

试试这个代码

public class MainActivity extends Activity { 
    private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView; 
private File f; 
public File getAlbumDir() 
{ 

    File storageDir = new File(
      Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES 
      ), 
      "BAC/" 
     ); 
    // Create directories if needed 
    if (!storageDir.exists()) { 
     storageDir.mkdirs(); 
    } 

    return storageDir; 
} 
private File createImageFile() throws IOException { 
    // Create an image file name 

    String imageFileName =getAlbumDir().toString() +"/image.jpg"; 
    File image = new File(imageFileName); 
    return image; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    this.imageView = (ImageView)this.findViewById(R.id.imageView1); 
    Button photoButton = (Button) this.findViewById(R.id.button1); 
    photoButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      try { 
       f = createImageFile(); 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
       startActivityForResult(cameraIntent, CAMERA_REQUEST); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 



     } 
    }); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

     Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath()); 
     imageView.setImageBitmap(photo); 

     Intent i = new Intent(Intent.ACTION_SEND); 
     i.setType("message/rfc822"); 
     i.putExtra(Intent.EXTRA_EMAIL , new String[]{"email id"}); 
     i.putExtra(Intent.EXTRA_SUBJECT, "subject"); 
     i.putExtra(Intent.EXTRA_TEXT , "body of email"); 

     Uri uri = Uri.fromFile(f); 
     i.putExtra(Intent.EXTRA_STREAM, uri); 
     try { 
      startActivity(Intent.createChooser(i, "Send mail...")); 
     } catch (android.content.ActivityNotFoundException ex) { 
      Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

}

+1

谢谢如此令人难以置信的多!这一直让我疯狂! – scb998 2014-09-30 01:10:48

0
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

emailIntent.setType("image/*"); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"go on read the emails");  
emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 
startActivity(Intent.createChooser(emailIntent, "Send Mail"));; 

这里,

uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), FILENAME)); 
+0

你是如何实现这个的? – scb998 2014-09-29 04:42:51

+0

是什么?你能否请你说说你想知道的东西?你有解决这个问题吗? – 2014-09-29 05:19:01

相关问题