我的代码中有位图,我需要通过电子邮件将其作为附件发送。
我将其保存为文件并进行发送,但是每次遇到错误时(找不到文件)。
这是我的代码。
保存文件:
private void savePicture(String filename, Bitmap b, Context ctx) {
try {
FileOutputStream out;
out = ctx.openFileOutput(filename, Context.MODE_APPEND);
b.compress(Bitmap.CompressFormat.JPEG, 40, out);
if (b.compress(Bitmap.CompressFormat.JPEG, 40, out) == true) {
Toast.makeText(act,"file created",Toast.LENGTH_LONG).show();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(act,e.getMessage(),Toast.LENGTH_LONG).show();
}
}
发送电子邮件:
public void sendmail (String filename){
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path,filename+".JPEG");
Uri pngUri = Uri.fromFile(file);
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);
startActivity(Intent.createChooser(emailIntent,"send quotation"));
}
答案 0 :(得分:1)
首先做
b.compress(Bitmap.CompressFormat.JPEG, 40, out);
if (b.compress(Bitmap.CompressFormat.JPEG, 40, out) == true) {
您打过两次电话b.compress(Bitmap.CompressFormat.JPEG, 40, out);
您应该执行类似boolean success = b.compress(Bitmap.CompressFormat.JPEG, 40, out);
if(success){
// Your code
}
表示您要定位Android N
或更高版本?如果是,您可能需要grant uri permissions
请参阅官方文档中的File providers and grant permissions