Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Intent.ACTION_SEND with EXTRA_STREAM doesn't attach any image when choosing Gmail app on htc Hero

On the Emulator with a default mail-app all works fine. But I have no attach when I'am receiving a mail which I've sent from my Hero using a Gmail app. The default Mail app on the hero works fine.

How can I make this code works with Gmail app on Hero?
You can see the code below.

    private void startSendIntent() {
        Bitmap bitmap = Bitmap.createBitmap(editableImageView.getWidth(), editableImageView.getHeight(), Bitmap.Config.RGB_565);
        editableImageView.draw(new Canvas(bitmap));
        File png = getFileStreamPath(getString(R.string.file_name));
        FileOutputStream out = null;
        try {
            out = openFileOutput(getString(R.string.file_name), MODE_WORLD_READABLE);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
            }
            catch (IOException ignore) {}
        }
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(png));
        emailIntent.setType("image/png");
        startActivity(Intent.createChooser(emailIntent, getString(R.string.send_intent_name)));
}

in Logs I see the following:

02-05 17:03:37.526: DEBUG/Gmail(11511): URI FOUND:file:///sdcard/DCIM/100MEDIA/IMAG0001.jpg
02-05 17:03:37.535: DEBUG/Gmail(11511): ComposeActivity added to message:0 attachment:|IMAG0001.jpg|image/jpeg|0|image/jpeg|LOCAL_FILE|file:///sdcard/DCIM/100MEDIA/IMAG0001.jpg size:0
02-05 17:03:37.585: INFO/Gmail(11511): >>>>> Attachment uri: file:///sdcard/DCIM/100MEDIA/IMAG0001.jpg
02-05 17:03:37.585: INFO/Gmail(11511): >>>>>           type: image/jpeg
02-05 17:03:37.585: INFO/Gmail(11511): >>>>>           name: IMAG0001.jpg
02-05 17:03:37.585: INFO/Gmail(11511): >>>>>           size: 0

Thank you for the answer.

like image 247
shinydev Avatar asked Feb 05 '10 10:02

shinydev


People also ask

What is Android intent Action view?

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.

What is intent setType in Android?

setType(String mimeType) input param is represent the MIME type data that u want to get in return from firing intent(here myIntent instance). by using one of following MIME type you can force user to pick option which you desire. Please take a Note here, All MIME types in android are in lowercase.

What is Share intent Android?

Android uses Intents and their associated extras to allow users to share information quickly and easily, using their favorite apps. Android provides two ways for users to share data between apps: The Android Sharesheet is primarily designed for sending content outside your app and/or directly to another user.


2 Answers

For me the problem was solved with the following lines of code:

Bitmap screenshot = Bitmap.createBitmap(_rootView.getWidth(), _rootView.getHeight(), Bitmap.Config.RGB_565);
_rootView.draw(new Canvas(screenshot));

String path = Images.Media.insertImage(getContentResolver(), screenshot, "title", null);
Uri screenshotUri = Uri.parse(path);

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
emailIntent.setType("image/png");

startActivity(Intent.createChooser(emailIntent, "Send email using"));

The key thing is that I'm saving the screen-shot to the media library and then it can successfully send a file from there.

like image 57
Aleks N. Avatar answered Nov 15 '22 18:11

Aleks N.


getFileStreamPath() or openFileOutput() will create files in a private directory that is inaccessible to other apps (i.e. Gmail). Use external storage to create publicly-accessible files:

private static final int REQUEST_SHARE = 39714;

private File png = null;

private void startSendIntent() {
    png = new File(new File(Environment.getExternalStorageDirectory(), "Pictures"), getString(R.string.file_name));

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(png);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) out.close();
        }
         catch (IOException ignore) {}
        }
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(png));
        emailIntent.setType("image/png");
        startActivityForResult(Intent.createChooser(emailIntent, getString(R.string.send_intent_name)), REQUEST_SHARE);
    }
}

This will not work when the external storage is unavailable, like when it is mounted as a USB drive. See the Storage API Guide for more about detecting whether external storage is available.

If your minimum API level is 8 or above, you can use Context.getExternalCacheDir() or Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) as the parent directory.

Unless you end up using getExternalCacheDir(), make sure you use a unique filename to prevent one of the users' files from accidentally getting overwritten.

Finally, you can override onActivityResult() to delete the file after the share operation completes:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == REQUEST_SHARE) {
        if(this.png == null) {
            return;
        }

        this.png.delete();
    }
}
like image 26
robinj Avatar answered Nov 15 '22 18:11

robinj