电子邮件Intent发送文本文件而不是图像

时间:2011-08-06 00:44:07

标签: java android android-image

这是我用来拍照,显示,然后通过电子邮件发送的代码:

private void takePicture() {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),
            getIntent().getStringExtra("counter"));
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, 0);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 0:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ContentResolver cr = getContentResolver();
            try {
                bitmap = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, selectedImage);

                savePicture(getIntent().getStringExtra("counter"), bitmap,
                        getApplicationContext());
                setImage();
                Toast.makeText(this, selectedImage.toString(), Toast.LENGTH_LONG).show();
            } catch (Exception e) {

            }
        }
    }
}

我如何发送电子邮件:

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


            String fileName = null;
            try {
                fileName = URLEncoder.encode(getIntent().getStringExtra("counter"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            String path =  Environment.getExternalStorageDirectory()+"/"+fileName.trim().toString();    
             Uri uri = Uri.parse("file://"+path);

             emailIntent.setType("image/png");
            emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri);


                    /* Send it off to the Activity-Chooser */
            startActivity(Intent
                    .createChooser(emailIntent, "Send Email..."));

请注意,我的文件名应为getIntent().getStringExtra("counter");

当我这样做时,我的电子邮件会发送文本文件而不是图像文件。我不知道为什么会这样做......

1 个答案:

答案 0 :(得分:0)

我怀疑这发生在不止一个人身上,但问题是:

我必须将 +".jpg" 添加到文件名的末尾,现在它可以正常工作。这可能是一个简单的解决方案,但肯定是一个令人痛苦的问题!

希望这篇文章能够帮助那些在将来遇到这个问题的人。