2012-08-23 85 views
0

我正在嘗試創建一個應用程序,您可以在該應用程序中拍照並將其通過電子郵件發送給某人。目前,我可以拍照,並設置我的背景是這樣的畫面:拍照然後通過電子郵件發送

public class Camera extends Activity implements View.OnClickListener{ 


ImageButton ib; 
Button b; 
ImageView iv; 
Intent i; 
final static int cameraData = 0; 
Bitmap bmp; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.photo); 
    initialize(); 
    InputStream is = getResources().openRawResource(R.drawable.ic_launcher); 
    bmp = BitmapFactory.decodeStream(is); 
} 

private void initialize(){ 
    ib = (ImageButton) findViewById(R.id.ibTakePic); 
    b = (Button) findViewById(R.id.bSetWall); 
    iv = (ImageView) findViewById(R.id.ivReturnedPic); 
    b.setOnClickListener(this); 
    ib.setOnClickListener(this); 



} 

@Override 
public void onClick(View v) { 
    File mImageFile; 
    // TODO Auto-generated method stub 
    switch(v.getId()){ 
    case R.id.bSetWall: 
     try { 
      getApplicationContext().setWallpaper(bmp); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    break; 
    case R.id.ibTakePic: 
     i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(i, cameraData); 
    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); 
    } 
} 




} 

我有一個單獨的應用程序,我可以在用戶輸入並通過電子郵件發送到預定義地址:

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated(); 
    String emailaddress[] = { "[email protected]", "", }; 
    String message = emailAdd + name + beginning; 

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress); 

    emailIntent.setType("plain/text"); 
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); 
    startActivity(emailIntent); 

} 

我如何去發送我拍攝的照片?它在哪裏保存,我如何訪問它以便我可以通過電子郵件發送?

非常感謝

回答

1
@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
     if(resultCode == RESULT_OK){ 
    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 
       File output = new File(dir, "camerascript.png"); 
       cPath = output.getAbsolutePath(); 
       Bitmap b = BitmapFactory.decodeFile(cPath); 
       Bitmap out = Bitmap.createScaledBitmap(b, 320, 480, false); 
       FileOutputStream fout; 
       try{ 
        fout = new FileOutputStream(output); 
        out.compress(Bitmap.CompressFormat.PNG, 100, fout); 
        fout.flush(); 
        fout.close(); 
        b.recycle(); 
        out.recycle(); 
       }catch(Exception e){ 
        e.printStackTrace(); 
       } 

     } 
    } 

在你發送郵件的方法

public void sendMail(){ 
     Log.e("sendMail", "v r in sendMail"); 

     i = new Intent(Intent.ACTION_SEND); 
     i.setType("plain/text"); 
      i.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"}); 
      i.putExtra(Intent.EXTRA_SUBJECT,"subject..."); 
     i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cPath)); 
     i.putExtra(Intent.EXTRA_TEXT, "Body of Email...."); 
     startActivityForResult(Intent.createChooser(i, "send mail...."),EMAIL_SUCCESS); 

    } 

上面的代碼將幫助ü..

+0

當我嘗試使用onActivityResult方法時,獲取以下錯誤:java.lang.RuntimeException:傳遞結果失敗ResultInfo {who = null,request = 0,result = -1,data = Intent {act = inline-data (有額外)}}}活動{com.thenewboston.david/com.thenewboston.david.Camera}:java.lang.NullPointerException。 – DMC

0
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode == RESULT_OK){ 
    Uri uri=data.getData(); 
    sendmail(uri); 

}

and 
    private void sendmail(Uri uri) 
    { 
    i = new Intent(Intent.ACTION_SEND); 
    i.setType("plain/text"); 
    i.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"}); 
    i.putExtra(Intent.EXTRA_SUBJECT,"subject..."); 
    i.putExtra(Intent.EXTRA_STREAM, uri); 
    i.putExtra(Intent.EXTRA_TEXT, "Body of Email...."); 
    startActivityForResult(Intent.createChooser(i, "send mail...."),EMAIL_SUCCESS); 

    } 

它可能有效。