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"
however the file size is only 69 kb, according to my print statement...
from Json file not being attached to email for android
No comments:
Post a Comment