0

Developing Android email plugin for Unity. I have a screenshot in the files/ folder of the app, I want to attach to mail. As it turned out, I cannot attach from there directly. I implemented a FileProvider, but it turned out that it exist only above 4.0.

So I implemented the suggested workaround, to save it to external storage, then attach from there. Saving seems work, even reading seems work, but still, Gmail says "Can't attach empty file". Also When launching email intent, I have an error message, like:

E/HwEmailTag( 7327): AttachmentUtilities->inferMimeTypeForUri->Unable to determine MIME type for uri=/storage/emulated/0/com.eppz.plugins_screenshot.jpg

I tried application/image, image/jpg as intent.setType(), still the same, while Gmail says the file is empty.

Is this something with emulated external storage /storage/emulated/0/? The device has no SD card, but I've read that getExternalStorage() returns a shared / public place for files in such cases either.

It should work. Should I remove dots from filename? Hope not. Here's the corresponding code:

String saveImageAtPathToExternalStorage(String imagePath)
{
    Log.i(TAG, "saveImageAtPathToExternalStorage(...)");

    // Create bitmap.
    File imageFile = new File(imagePath);
    Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), new BitmapFactory.Options());

    // Output.
    String outputFileName = _unityPlayerActivity.getPackageName()+"_screenshot.jpg";
    String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File outputImageFile = new File(externalStorageDirectory, outputFileName);
    String outputImagePath = outputImageFile.getAbsolutePath();
    if (outputImageFile.exists()) outputImageFile.delete(); // Delete if existed

    try
    {
        // Write JPG.
        FileOutputStream outputStream = new FileOutputStream(outputImageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();

        Log.i(TAG, "Image written to `"+outputImagePath+"`");
    }
    catch (Exception e)
    { e.printStackTrace(); }

    // Return with output path.
    return outputImagePath;
}

public void openMailComposer(String to, String subject, String body, int isHTML, String attachmentImagePath)
{
    Log.i(TAG, "openMailComposer(...)");

    // Attachment image.
    File attachmentImageFile = new File(attachmentImagePath);
    if (attachmentImageFile.exists() == false)
    {
        Log.i(TAG, IMAGE_NOT_FOUND);
        SendUnityMessage(OPEN_MAIL_COMPOSER_CALLBACK_METHOD_NAME, IMAGE_NOT_FOUND);
        return;
    }

    // Save to external first.
    String externalImagePath = saveImageAtPathToExternalStorage(attachmentImagePath);
    final Uri externalImageUri = Uri.parse(externalImagePath);

    Log.i(TAG, "externalImageUri `"+externalImageUri+"`");

    // Intent.
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/jpeg");
    intent.putExtra(Intent.EXTRA_EMAIL, to);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, body);
    if (isHTML == 1) intent.putExtra(Intent.EXTRA_HTML_TEXT, body);

    // Attach.
    intent.putExtra(Intent.EXTRA_STREAM, externalImageUri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    startActivityForResult(Intent.createChooser(intent, "Send email"), OPEN_MAIL_COMPOSER_REQUEST_CODE);
}

I'm relatively new to Android development, and really want to believe, but having all this hassle compared to having a single line for this in iOS is quiet distressing.

I'm running this on a Huawei MediaPad (TT1 7.0), Android 4.4.2, and I want it to run about Android 2.3+ basically (why I refused using FileProvider earlier).

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
  • "I implemented a FileProvider, but it turned out that it exist only above 4.0" -- `FileProvider` works going back to API Level 4 = Android 1.6. – CommonsWare Mar 07 '16 at 17:18
  • Wow that would be great. However, this answer states it not working even in 4.0 http://stackoverflow.com/a/21223590/215282 I stopped at this point, shouldn't I? – Geri Borbás Mar 07 '16 at 17:26
  • I am not aware of any problems with `FileProvider` on older versions of Android. *Clients* of `FileProvider` have their issues, and there may be more of those issues on older devices, if those apps have older clients (e.g., older Gmail). – CommonsWare Mar 07 '16 at 17:45

0 Answers0