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

Roy Weil

Lurker
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>
 
Top