2012-05-25 58 views
0

我試圖創建一個應用程序,當按下按鈕時拍攝照片,然後在按下發送按鈕時將其附加到電子郵件。我有的代碼顯示圖片附加到電子郵件中,但是當我檢查我發送給它的電子郵件時,附件不會被髮送。圖像也不會保存在SD卡中。Android:保存從應用程序拍攝的照片並將其附加到電子郵件

我在我的onCreate()中調用初始化函數來初始化變量。我不知道在哪裏放置SaveImage()函數,它必須在拍攝後保存圖像。我把它放在onActivityResult()中,但不起作用。 在此先感謝!

private void initialize() { 
    // TODO Auto-generated method stub 
    camera = (Button) findViewById(R.id.Picture); 
    sendEmail = (Button) findViewById(R.id.SendMessage); 
    iv = (ImageView) findViewById(R.id.ImageReturn); 
    MessageTyped = (EditText) findViewById(R.id.MessageField); 
    sendEmail.setOnClickListener(this); 
    camera.setOnClickListener(this); 

    pngDir = new File(Environment.getExternalStorageDirectory(), 
    // Loose convention inferred from app examples 
      "My Images"); 

    if (!pngDir.exists()) 
     pngDir.mkdirs(); 

} 

@Override 
public void onClick(View v) { 

    // TODO Auto-generated method stub 

    switch (v.getId()) { 

    case R.id.Picture: 
     i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     SaveImage();//<------------------WHERE DOES THIS GO? 

     startActivityForResult(i, cameraData); 

     break; 

    case R.id.SendMessage: 

     EditTextToString(); 

     EmailIntent = new Intent(android.content.Intent.ACTION_SEND); 

     EmailIntent.putExtra(Intent.EXTRA_EMAIL, 
       new String[] { "bi[email protected]" }); 
     EmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
       MessageToBeReceived); 

     // EmailIntent.setType("message/rfc822"); 
     EmailIntent.setType("image/jpeg"); 
     EmailIntent.putExtra(Intent.EXTRA_STREAM, pngUri); 

     startActivity(Intent.createChooser(EmailIntent, 
       "Choose an Email client :")); 
     break; 

    } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     bmp = (Bitmap) extras.get("data"); 
     iv.setImageBitmap(bmp); 

    } 
} 

private void EditTextToString() { 
    MessageToBeReceived = MessageTyped.getText().toString(); 
} 

private void SaveImage() { 

    File pngFile = new File(pngDir, "jetsam.jpeg"); 
    // Save file encoded as PNG 
    pngUri = Uri.fromFile(pngFile); 

} 

回答

0

查看ACTION_IMAGE_CAPTURE的文檔!如果你沒有給出EXTRA_OUTPUT(uri和保存圖像的位置),結果你會得到「小圖像作爲一個Bitmap對象返回」。

所以剛纔URI請求圖片時添加的意圖,並通過同一個URI來ACTION_SEND:

case R.id.Picture: 
    i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    i.putExtra(MediaStore.EXTRA_OUTPUT, getUriForNewFile()); // you have to implement youself getUriForNewFile() 

    startActivityForResult(i, cameraData); 

    break; 
相關問題