2012-08-29 49 views
1

我有一個應用程序可以拍攝照片並將照片內部存儲在我創建的文件夾中。拍完照片後,我希望能夠訪問它,以便我可以通過電子郵件發送。 ?我怎樣才能訪問我剛纔拍攝的圖像下面是我的代碼,保存圖像拍攝完畢後,在內部的圖像:如何訪問內部存儲的圖像

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     bmp = (Bitmap) extras.get("data"); 
     iv.setImageBitmap(bmp); 

     File storagePath = new File(
       Environment.getExternalStorageDirectory() + "/DavePics/"); 
     storagePath.mkdirs(); 

     File myImage = new File(storagePath, Long.toString(System 
       .currentTimeMillis()) + ".jpg"); 


     Bitmap b = Bitmap.createScaledBitmap(bmp, 320, 480, false); 

     try { 
      FileOutputStream out = new FileOutputStream(myImage); 
      b.compress(Bitmap.CompressFormat.JPEG, 80, out); 
      out.flush(); 
      out.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

} 

當我檢查我創建了圖片的文件夾是存在的。我想現在要做的就是訪問圖片,這樣我可以在一封電子郵件從下面我的代碼發送:

@Override 
public void onClick(View v) { 

    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.bSendPic: 

     String emailaddress[] = { "[email protected]", "", }; 

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

     //emailIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, pic); 

     emailIntent.setType("image/jpeg"); 
     startActivity(Intent.createChooser(emailIntent, "Send Mail")); 

     break; 
    case R.id.ibTakePic: 

     i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

     startActivityForResult(i, cameraData); 
     break; 
    } 

} 

如何訪問的這張照片,這樣我可以把它添加到我的電子郵件意圖是什麼?我是否正確地做這件事?由於

編輯:這是我的全部代碼

public class Camera extends Activity implements View.OnClickListener { 

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

@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.bSendPic); 
    iv = (ImageView) findViewById(R.id.ivReturnedPic); 
    b.setOnClickListener(this); 
    ib.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 

    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.bSendPic: 

     if (myImage.exists()) { 

      String emailaddress[] = { "[email protected]", "", }; 

      Intent emailIntent = new Intent(Intent.ACTION_SEND); 
      emailIntent.putExtra(Intent.EXTRA_EMAIL, emailaddress); 
      emailIntent.setType("image/jpeg"); 
      emailIntent 
        .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage)); 
      startActivity(Intent.createChooser(emailIntent, "Send Mail")); 

     } 

     break; 
    case R.id.ibTakePic: 

     i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

     startActivityForResult(i, cameraData); 
     break; 
    } 

} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     bmp = (Bitmap) extras.get("data"); 
     iv.setImageBitmap(bmp); 

     File storagePath = new File(
       Environment.getExternalStorageDirectory() + "/DavePics/"); 
     storagePath.mkdirs(); 

     myImage = new File(storagePath, Long.toString(System 
       .currentTimeMillis()) + ".jpg"); 

     Bitmap b = Bitmap.createScaledBitmap(bmp, 320, 480, false); 

     try { 
      FileOutputStream out = new FileOutputStream(myImage); 
      b.compress(Bitmap.CompressFormat.JPEG, 80, out); 
      out.flush(); 
      out.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

} 
+0

http://stackoverflow.com/questions/587917/trying-to-attach-a-file-from-sd-card-to-email – manjusg

回答

2

的代碼行,你就是忘了,,

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage)); 

在你的代碼,您的活動申報File myImage全球,

現在at Email發送代碼

檢查文件是否存在,

if(myImage.exist()) 
{ 
String emailaddress[] = { "[email protected]", "", }; 
Intent emailIntent = new Intent(Intent.ACTION_SEND); 
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailaddress); 
emailIntent.setType("image/jpeg"); 
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImage)); 
startActivity(Intent.createChooser(emailIntent, "Send Mail")); 
} 
+0

感謝我這樣做,當我去發送電子郵件你可以看到附件已被添加。但是,當我發送電子郵件時,它只是說電子郵件發送,然後它永遠不會發送。我已經提交了完整的代碼。有任何想法嗎? – DMC

+0

我知道這個工作。太感謝了!我的Gmail帳戶一直凍結,但一旦我清除緩存並重新啓動它,那麼它的工作完美! – DMC

+0

歡迎夥伴..!快樂編碼..! – user370305

相關問題