5

I am trying to send an attached json file to an email, however for some reason the json file is not being attached when the email is sent/created. Note: I do NOT want the user to select the file to attach, I want it to be fixed/set automatically.

I have the following permissions in my AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

and the code

   private void backupJsonToEmail(String jsonString) {
    // create file

    if(!getFilesDir().exists()){
        getFilesDir().mkdir();
    }
    String filePath = getFilesDir() + File.separator + BACKUP_NAME;
    System.out.println("file path: " + filePath);
    // /data/user/0/com.my.stuff/files/backup.json

    try {
        FileOutputStream fos = new FileOutputStream(filePath);
        DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
        outStream.writeBytes(jsonString);
        outStream.close();

        // send to email
        try {
            File file = new File(filePath);
            long fileKbSize = file.length() / 1024;
            System.out.println("FILE SIZE IS: " + fileKbSize + " kb"); // 69 kb...

            Uri uri = Uri.fromFile(file);
            String to[] = {"test@yahoo.com"};

            Intent originalIntent = ShareCompat.IntentBuilder.from(this)
                    .setType("application/json")
                    .setEmailTo(to)
                    .setStream(uri)
                    .setSubject("test")
                    .setText("here is the attached json")
                    .getIntent();
            originalIntent.setData(Uri.parse("mailto:"));
            originalIntent.setAction(Intent.ACTION_SENDTO);

            Intent finalIntent = Intent.createChooser(originalIntent, "choose an email application");
            startActivity(finalIntent);

        } catch (Throwable t) {
            Toast.makeText(this, "Request failed try again: " + t.toString(), Toast.LENGTH_LONG).show();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

EDIT1: after making the suggestion changes that @piyushpk suggested, I now get the following errors when I choose an email app:

for Yahoo Mail: "The attachment is too big to send"
for Gmail: "Permission denied for the attachment"

enter image description here

however the file size is only 69 kb, according to my print statement...

General Grievance
  • 4,555
  • 31
  • 31
  • 45
mr nooby noob
  • 1,860
  • 5
  • 33
  • 56
  • Hey @mr nooby noob, if your project have compile and target sdk version is 29 then add android:requestLegacyExternalStorage="true" inside application tag in Manifest – piyushpk Nov 09 '20 at 11:00
  • hey @piyushpk sorry for the late response but my compileSdkVersion and targetSdkVersion are 26. – mr nooby noob Nov 14 '20 at 15:04
  • also doing that, did not attach the file still, sadly – mr nooby noob Nov 14 '20 at 15:45
  • You should be crashing with a `NullPointerException` on Android 7.0+. Use `FileProvider`, not `Uri.fromFile()`, to create the `Uri` to share. And note that `ACTION_SENDTO` is not documented to support attachments. – CommonsWare Nov 14 '20 at 17:04
  • @CommonsWare I am actually NPE when I make that change: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference – mr nooby noob Nov 15 '20 at 20:27
  • 3
    Your authority string that you are passing into `getUriForFile()` does not match the authority string in your `` element. See https://stackoverflow.com/a/57946354/115145 – CommonsWare Nov 15 '20 at 20:36
  • 1
    @CommonWare thanks, I was actually able to getting working after I fixed that as well as making the change from here: https://stackoverflow.com/a/34389637/2403836, if you make a answer with what you suggested as well as my link, I can give you the reward. – mr nooby noob Nov 15 '20 at 20:39

1 Answers1

0

I think you are trying to use Inbuilt email application to send a json file and yahoo is complaining about size and Gmail is denying it as json is considered not a safe extension.

Instead use Some SMTP Email api like send grid etc. to send file without any inbuilt android application. https://github.com/sendgrid/sendgrid-java

Mail Gun Sendgrid are quite good option

Jin Thakur
  • 2,711
  • 18
  • 15