android share images from assets folder

Buda Gavril picture Buda Gavril · May 4, 2011 · Viewed 18.9k times · Source

I'm trying to share an image from my assets folder. My code is:

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/myImage.jpg"));
startActivity(Intent.createChooser(share, "Share This Image"));

but it doesn't work. Do you have any ideas?

Answer

smith324 picture smith324 · Aug 24, 2011

It is possible to share files (images including) from the assets folder through a custom ContentProvider

You need to extend ContentProvider, register it in your manifest and implement the openAssetFile method. You can then assess the assets via Uris

    @Override
    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
        AssetManager am = getContext().getAssets();
        String file_name = uri.getLastPathSegment();

        if(file_name == null) 
            throw new FileNotFoundException();
        AssetFileDescriptor afd = null;
        try {
            afd = am.openFd(file_name);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return afd;
    }