Gmail 5.0 app fails with “Permission denied for the attachment” when it receives ACTION_SEND intent

后端 未结 9 1221
感动是毒
感动是毒 2020-11-27 18:35

My app creates mails with attachments, and uses an intent with Intent.ACTION_SEND to launch a mail app.

It works with all the mail apps I tested with, e

9条回答
  •  没有蜡笔的小新
    2020-11-27 19:22

    I was having this problem and finally found an easy way to send email with attachment. Here is the code

    public void SendEmail(){
        try {
    
            //saving image
            String randomNameOfPic = Calendar.DAY_OF_YEAR+DateFormat.getTimeInstance().toString();
            File file = new File(ActivityRecharge.this.getCacheDir(), "slip"+  randomNameOfPic+ ".jpg");
            FileOutputStream fOut = new FileOutputStream(file);
            myPic.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.flush();
            fOut.close();
            file.setReadable(true, false);
    
            //sending email
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"zohabali5@gmail.com"});
            intent.putExtra(Intent.EXTRA_SUBJECT, "Recharge Account");
            intent.putExtra(Intent.EXTRA_TEXT, "body text");
    
            //Uri uri = Uri.parse("file://" + fileAbsolutePath);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivityForResult(Intent.createChooser(intent, "Send email..."),12);
        }catch (Exception e){
            Toast.makeText(ActivityRecharge.this,"Unable to open Email intent",Toast.LENGTH_LONG).show();
        }
    }
    

    In this code "myPic" is bitmap which was returned by camera intent

提交回复
热议问题