0

我在 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 上进行测试,如果它有什么不同的话。

4

1 回答 1

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"));
于 2013-08-23T18:39:49.083 回答