2

I have a function that I want to automatically take a screenshot and then upload it to the users preferred email app.

    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {
        // image naming and path  to include sd card  appending name you choose for file
        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".png";

        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.PNG, quality, outputStream);
        outputStream.flush();
        outputStream.close();
        File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + now + mPath);
        Uri path = Uri.fromFile(filelocation);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        // set the type to 'email'
        emailIntent .setType("vnd.android.cursor.dir/email");
        String to[] = {"Enter your email address"};
        emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
        // the attachment
        emailIntent.putExtra(Intent.EXTRA_STREAM, path);
        // the mail subject
        emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Journey : ");
        startActivity(Intent.createChooser(emailIntent , "Select your preferred email app.."));


    } catch (Throwable e) {
        // Several error may come out with file handling or DOM
        e.printStackTrace();
    }
}

This is my function. It is taking the screen shot automatically and its store it on my local device. It is also prompting the user to select their email app. Then I select an app it says "unable to attach file" . I have read and write permissions in my manifest.

tadman
  • 208,517
  • 23
  • 234
  • 262
JamesD
  • 98
  • 7
  • You should tag this with whatever language is used. In this case is it Java? – tadman Mar 21 '18 at 21:31
  • @tadman: No, you should tag it with the language tag only if the question is about the language. – CommonsWare Mar 21 '18 at 21:38
  • @CommonsWare If it's Java code, tagging it with Java usually helps get the right attention. It also explains what it is better than something really generic like "Android" which could be Kotlin, etc. – tadman Mar 21 '18 at 21:40
  • @tadman: Only at the cost of completely polluting the language tag. Do you really think that the people helping out on Java want to see every random Android question, every random Spring Boot question, etc.? – CommonsWare Mar 21 '18 at 21:42
  • @CommonsWare That's how we roll in pretty much every other language tag, so yeah. If it has substantial amounts of Java code it's a at least partially a Java question. – tadman Mar 21 '18 at 21:43

3 Answers3

1

The other app may not have access to external storage. Plus, your code will crash on Android 7.0+, once you raise your targetSdkVersion to 24 or higher.

Switch to using FileProvider and its getUriForFile() method, instead of using Uri.fromFile().

And, eventually, move that disk I/O to a background thread.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

check this link -

https://www.javacodegeeks.com/2013/10/send-email-with-attachment-in-android.html

and How to send an email with a file attachment in Android

Hope this help.

Asteroid
  • 718
  • 7
  • 21
0

The problem was :

Uri path = Uri.fromFile(filelocation);

Instead I used :

File filelocation = new File(MediaStore.Images.Media.DATA + mPath);

Uri myUri = Uri.parse("file://" + filelocation);

Hopefully this helps anyone facing the same problem.

JamesD
  • 98
  • 7