1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by Roy Weil, Oct 2, 2021.

  1. Roy Weil

    Roy Weil Lurker
    Thread Starter

    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>
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - android app send
  1. GiuseppeSM
    Replies:
    3
    Views:
    377
  2. Epic555
    Replies:
    2
    Views:
    309
  3. Towson_Steve
    Replies:
    10
    Views:
    591
  4. Six dots Software
    Replies:
    2
    Views:
    289
  5. James Tracy
    Replies:
    3
    Views:
    309
  6. rbjhb
    Replies:
    2
    Views:
    592
  7. Aby PAul
    Replies:
    0
    Views:
    452
  8. July Hoang
    Replies:
    14
    Views:
    939
  9. werner111
    Replies:
    6
    Views:
    1,263

Share This Page

Loading...