Android assest文件夹图像附件到电子邮件失败
我正在尝试向邮件发送附加assest文件夹图像的邮件,邮件已成功发送,但当我选中它时,邮件中没有附加图像, 这是我的密码Android assest文件夹图像附件到电子邮件失败,android,Android,我正在尝试向邮件发送附加assest文件夹图像的邮件,邮件已成功发送,但当我选中它时,邮件中没有附加图像, 这是我的密码 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/p
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" />
您只能从SD卡附加文件。 您需要将图像复制到SD卡,并更改SD卡上文件路径的URI。
您可以在以后删除if。如果需要,您可以这样做
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);