views:

85

answers:

3

I am developing an application that displays images. The application provides an option to email the image as an attachment. I have to save the image to the SD card in order to email the image as an attachment.

Is it possible to email the image as an attachment without writing it to the SD card first?

A: 

AFAIK, writting to the SDCard fist is the only way to do it... and it's necessary since the mail client has to be able to read the file; so, it has to be stored on a public place.

Cristian
+1  A: 

You have to create a ContentProvider for your images

fedj
@can u give elaborate answer,or give sample code for me
sivaraj
A: 

@sivaraj For example, if you store you image anywhere in the phone (even in a private folder) Just modify it if you don't want to store it at all in the openFile method. Register your provider in your manifest with the right authorities attribute and that's it !

public class ImageProvider extends ContentProvider {
        private static final String URI_PREFIX = "content://com.android.myproject.anythingyouwant";

        public static String constructUri(String url) {
            Uri uri = Uri.parse(url);
            return uri.isAbsolute() ? url : URI_PREFIX + url;
        }

        @Override
        public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
            File file = new File(uri.getPath());
            ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
            return parcel;
        }

        @Override
        public boolean onCreate() {
            return true;
        }

        @Override
        public int delete(Uri uri, String s, String[] as) {
            throw new UnsupportedOperationException("Not supported by this provider");
        }

        @Override
        public String getType(Uri uri) {
            throw new UnsupportedOperationException("Not supported by this provider");
        }

        @Override
        public Uri insert(Uri uri, ContentValues contentvalues) {
            throw new UnsupportedOperationException("Not supported by this provider");
        }

        @Override
        public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
            throw new UnsupportedOperationException("Not supported by this provider");
        }

        @Override
        public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
           throw new UnsupportedOperationException("Not supported by this provider");
        }
    }
fedj
Hi @fedj, Hi @sivaraj, I am not entirely familiar with this approach, but I am trying to do the same thing as you. Can you share what you ended up implementing, and how I can use this ContentProvider to attach the image to the email? Thank you in advance!
Zarah