0

I have an image that is in the following location (it's in my android workspace resources),

D:\Android\WorkSpace\myprojectname\res\drawable-hdpi

I have used the following line of code to attach this image to an email but it doesn't seem to be working, it sends the email but it won't have the attachment.

emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,Uri.parse("android.resource://com.mywebsite.myprojectname/" + R.drawable.image));

this this wrong?

Mona
  • 5,939
  • 3
  • 28
  • 33

3 Answers3

1
    Resources res = this.getResources();
    Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.image);

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    File f = new File(Environment.getExternalStorageDirectory(), "image.jpg");

    try {
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //write the bytes in file
    FileOutputStream fo = null;
    try {
        fo = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Uri uri = Uri.fromFile(f);

After that,

Where u r sending the email, do the following,

    intent.putExtra(Intent.EXTRA_STREAM, uri);

It will work for sure. it worked for completely. Try it.

Debarati
  • 3,296
  • 2
  • 18
  • 30
0

Well, the first problem is that even if that URI format is right (which I doubt), that file would be inside your application's sandbox, which is not accessible to the E-mail activity (or any other activity, for that matter). In any case, you're gonna have to write that file into the SD card and have the email program read it from there. You can output the bitmap using the following code:

Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.image);
File file = new File(Environment.getExternalStorageDirectory(), "forEmail.PNG");

OutputStream outStream = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();

Then use Uri.fromFile() to generate the URI from the file defined above:

emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file));

(Code for resource to file adapted from here)

Community
  • 1
  • 1
dmon
  • 30,048
  • 8
  • 87
  • 96
  • on the android email page it shows that it has that image attached but on the reciever end there is no attachment with the email! – Mona Nov 15 '11 at 21:42
0

The other answers might work too (they didn't work for me though)! I got it running using only the one line of code bellow, which is very simple and easy. Thanks everyone.

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.image));

I think my issue was that I was putting the package name wrong, but using the getPackageName() method that issue is solved!

Mona
  • 5,939
  • 3
  • 28
  • 33