2011-11-22 88 views
0

我目前正在編寫一個應用程序,在某個活動中,我們希望用戶能夠拍攝並通過電子郵件將照片發送到所需的電子郵件地址。我可以單獨執行這兩項操作(拍照和發送照片),但是當我將它們一起運行時,電子郵件客戶端列表會出現在相機上...我似乎無法弄清楚爲什麼它沒有運行在相機本身之後..有什麼幫助?Android應用拍攝/電子郵件照片

***這是我現在有:

public class PhotoHandler extends Activity { 

private final static int TAKE_PHOTO_CODE = 1; 
File downloadedPic = null; 
Intent in; 

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

    downloadedPic = takeandReturn(this); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 

    try {    
     Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);    
     picMessageIntent.setType("image/jpeg"); 
     picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic)); 
     picMessageIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{}); 
     picMessageIntent.putExtra(Intent.EXTRA_SUBJECT, "MNWV - Check Out This Photo!"); 
     picMessageIntent.putExtra(Intent.EXTRA_TEXT , "*** Please Describe the Photo Taken Below (Include Your Name, Location, etc.)... ***"); 
     startActivity(Intent.createChooser(picMessageIntent, "Send Picture Using: ")); 
    } catch (Exception e) { 
     Log.e("TAG", "sendPictureMessage() failed to start activity.", e); 
     Toast.makeText(this, "No handler", Toast.LENGTH_LONG).show(); 
    } 
} 

回答

2

必須使用startActivityForResult採取的照片。之後,你必須使用onActivityResult發送電子郵件:

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     // TODO: Test for requestCode and resultCode 
     try {    
      Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);    
      picMessageIntent.setType("image/jpeg"); 
      picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic)); 
      startActivity(Intent.createChooser(picMessageIntent, "Send Picture Using: ")); 
     } catch (Exception e) { 
      Log.e("TAG", "sendPictureMessage() failed to start activity.", e); 
      Toast.makeText(this, "No handler", Toast.LENGTH_LONG).show(); 
     } 
    } 

希望這將有助於。

+0

你是什麼意思測試的重要代碼和結果代碼?我不明白這一點。即時通訊抱歉,即時通訊新的Android開發 – comead

+0

謝謝!我知道了! – comead

+0

當您必須在您的Activity中處理多個對startActivityForResult的調用時,requestCode纔有用:這意味着您必須檢查您在onActivityResult上收到的請求代碼是否與調用startActivityForResult時使用的請求代碼相匹配。關於resultCode,您必須檢查它與RESULT_CANCELED的不同。你可以在http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int,int,android.content.Intent) – Hicham