将图像附加到ACTION_SEND,然后删除

时间:2013-08-05 20:05:59

标签: android image email attachment delete-file

我在android中创建一个ACTION_SEND意图,并使用以下代码附加图像文件:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, "");
intent.putExtra(Intent.EXTRA_SUBJECT, "Receipt From: XXX");
intent.putExtra(Intent.EXTRA_TEXT, message);

byte[] b = Base64.decode(signature, Base64.DEFAULT); //Where signature is a base64 encoded string
final Bitmap mBitmap = BitmapFactory.decodeByteArray(b, 0, b.length);

String path = Images.Media.insertImage(getContentResolver(), mBitmap, "signature", null);
Uri sigURI = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, sigURI);
startActivity(Intent.createChooser(intent, "Send Email"));

这样可以,图像附加到电子邮件中。但是我在删除图像后遇到了问题。图像将存储在DCIM \ Camera文件夹中,文件名为。

我试过

File tempFile = File(getRealPathFromURI(sigURI)); //a subroutine which gives me the full path
tempFile.delete();

tempFile.delete返回true,但文件仍然存在。 我注意到一件奇怪的事情是保存的图像的文件大小为0,并且在我尝试删除之前和之后都显示为空。

如何在发送电子邮件后正确删除此图片?或者有没有另外一种方法来附加图像而不保存它?

此外,这不是主要问题,但如果你可以包括如何将图像/附件的名称从1375729812685.jpg(或者数字可能是什么)更改为其他内容,我会很感激。

作为最后一点,我一直在测试HTC Evo是否有任何区别。

1 个答案:

答案 0 :(得分:2)

我找到了感兴趣的人的解决方案。

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{});
intent.putExtra(android.content.Intent.EXTRA_CC, new String[ {"test@test.net"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Receipt From: " + receipt[1]);


String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
File albumF = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Receipt");

if (albumF != null) {
    if (! albumF.mkdirs()) {
        if (! albumF.exists()){
            Log.d("CameraSample", "failed to create directory");
            return;
         }
    }
}

File imageF = File.createTempFile(imageFileName, ".jpg", albumF);
byte[] b = Base64.decode(sig, Base64.DEFAULT);
if (b.length > 0) {
    Bitmap mBitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
    FileOutputStream ostream = new FileOutputStream(imageF);
    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
    ostream.flush();
    ostream.close();

    Uri fileUri = Uri.fromFile(imageF);

    intent.putExtra(Intent.EXTRA_STREAM, fileUri);
}


intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");

startActivity(Intent.createChooser(intent, "Send Email Using"));