0

我正在尝试向邮件发送附有资产文件夹图像的邮件,邮件已成功发送,但是当我检查它时,邮件中没有附加图像,这是我的代码,

Uri uri = Uri.fromFile(new File("file:///android_asset/Hat_5.png"));

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_EMAIL  , new String[] { "some@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "New Order");
intent.putExtra(Intent.EXTRA_TEXT   , "Order Id :" +imageId);
intent.putExtra(Intent.EXTRA_STREAM  , uri);
startActivity(Intent.createChooser(intent, "Send mail..."));

允许,

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
4

2 回答 2

0

您只能附加 SDCARD 中的文件。您需要将图像复制到 SDCARD 并将 URI 更改为 sdcard 上文件的路径。如果之后你可以删除。

于 2012-10-02T10:10:24.587 回答
0

如果你愿意,你可以这样做

String FILENAME = "avatar.png";
        FileOutputStream fos = null;

    try {

        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

    } catch (FileNotFoundException e1) {

        e1.printStackTrace();
        Log.v("","FileNotFoundException: "+e1.getMessage());

    }

    try {
        InputStream inputStream = getAssets().open("avatar.jpg");


        byte buf[]=new byte[1024];
        int len;
        while((len=inputStream.read(buf))>0)
            fos.write(buf,0,len);
        fos.close();
        inputStream.close();

    } catch (IOException e1) {

        e1.printStackTrace();
        Log.v("","IOException: "+e1.getMessage());

    }

    // Get the file from internal storage

    String filePath = getApplicationContext().getFilesDir().getAbsolutePath();//returns current directory.
    File file = new File(filePath, FILENAME);

然后

Uri uri = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM  , uri);

并发送附件...

于 2012-10-02T10:25:04.323 回答