• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

android app to send image via email fails with file not found or permissions

Roy Weil

Lurker
Oct 2, 2021
1
0
I am creating an android app to send an email with attached image. The mail arrives with no attachment. I have tried three ways to create the file. and three ways to send. the code returns exception "filenotfound" or some permission error. Below is an extraction of the code.
manifest has:
<code>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

private File storeImage(Bitmap image) {
File pic = new File("picdefulet.png");
String filename = "pic.png";
try {
switch (1) {
case 1:
pic = new
File(Environment.getExternalStorageDirectory(),
filename);
break;
case 2:
try {
File dirFile = getCacheDir();
boolean result = dirFile.mkdir();
pic = new File(getCacheDir(), filename);
} catch( Exception e){
Log.e("BROKEN", e.getMessage());
}
break;
case 3:
pic = new File(filename);
break;
}
try {
FileOutputStream out = new FileOutputStream(pic);
boolean result = image.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch( Exception e){
Log.e("BROKEN", e.getMessage());
}
}
catch (Exception e) {
Log.e("BROKEN", e.getMessage());
}
return pic;
} // end storeImage
String msgtext = "#22 Please warn/fine/terminate the user \\n\\n";
Bitmap photo = (Bitmap) data.getExtras().get("data");
File fileThing = storeImage(photo);

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("multipart/alternative;");
String[] recipient = new String[2];
recipient[0] = "roySpin@royweil.com";
recipient[1] = "roysoin2@royweil.com";
emailIntent.putExtra(Intent.EXTRA_EMAIL,recipient); // recipients
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Scooter Illegaly Parked");
emailIntent.putExtra(Intent.EXTRA_TEXT, msgtext);
if (!fileThing.getPath().isEmpty()) {
switch (1) {
case 1:
Uri pngUri = Uri.parse("file://" + fileThing);
break;
case 2:
Uri pngUri = Uri.fromFile ( fileThing);
break;
case 3:
pngUri = FileProvider.getUriForFile(
getApplicationContext(),
getApplicationContext().getPackageName() + ".provider",
fileThing);
break;
default:
throw new Exception("Invalid case for assigning pngUri");
}
mailIntent.putExtra(Intent.EXTRA_STREAM, pngUri);
gtartActivity(emailIntent);
</code>
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones