Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Reliably share an image from assets via messaging, G+, twitter, facebook?

I am trying to create a button in my android app that allows the user to share an image using their choice of social media network. The image file is stored in the assets folder of the app.

My plan is to implement a custom ContentProvider to give external access to the image, then send a TYPE_SEND intent specifying the uri of the image within my content provider.

I have done this and it works for Google+ and GMail, but for other services it fails. The hardest part has been finding information on what I'm supposed to return from the query() method of my ContentProvider. Some apps specify a projection (e.g. Google+ asks for _id and _data), while some apps pass null as the projection. Even where the projection is specified, I've no idea what actual data (types) are expected in the columns. I can find no documentation on this.

I have also implemented the openAssetFile method of the ContentProvider and this gets called (twice by Google+!) but then inevitably the query method get called as well. Only the result of the query method seems to count.

Any ideas where I'm going wrong? What should I be returning from my query method?

Code below:

// my intent

Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("image/jpeg");
Uri uri = Uri.parse("content://com.me.provider/ic_launcher.jpg");
i.putExtra(Intent.EXTRA_STREAM, uri);       
i.putExtra(android.content.Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(i, "Share via"));

// my custom content provider

public class ImageProvider extends ContentProvider
{
private AssetManager _assetManager;

public static final Uri CONTENT_URI = Uri.parse("content://com.me.provider");

// not called
@Override
public int delete(Uri arg0, String arg1, String[] arg2) 
{
    return 0;
}

// not called
@Override
public String getType(Uri uri) 
{
    return "image/jpeg";
}

// not called
@Override
public Uri insert(Uri uri, ContentValues values) 
{
    return null;
}

@Override
public boolean onCreate() 
{
    _assetManager = getContext().getAssets();
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 
{
    MatrixCursor c = new MatrixCursor(new String[] { "_id", "_data" });

    try
    {
                    // just a guess!! works for g+ :/
        c.addRow(new Object[] { "ic_launcher.jpg",  _assetManager.openFd("ic_launcher.jpg") });
    } catch (IOException e)
    {
        e.printStackTrace();
        return null;
    }

    return c;
}

// not called
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) 
{
    return 0;
}

// not called  
@Override
public String[] getStreamTypes(Uri uri, String mimeTypeFilter)
{

    return new String[] { "image/jpeg" };
}

// called by most apps
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException 
{

    try 
    {
        AssetFileDescriptor afd = _assetManager.openFd("ic_launcher.jpg");
        return afd;
    } catch (IOException e) 
    {
        throw new FileNotFoundException("No asset found: " + uri);
    }
}

// not called
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
        throws FileNotFoundException
{

    return super.openFile(uri, mode);
}

}

like image 489
felix Avatar asked Nov 14 '22 01:11

felix


1 Answers

Thanks, your question solved mine ;) I was having the exactly inverse problem of yours: every service would work except g+. I was returning null in the query method, that made g+ crash.

The only thing to actually expose my images was to implement openFile(). I have my images stored on the filesystem, not in the assets, but I suppose you could get a ParcelFileDescriptor from your AssetFileDescriptor and return it. My openFile() method looks like this:

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
        throws FileNotFoundException {
    String path = uri.getLastPathSegment();
    if (path == null) {
        throw new IllegalArgumentException("Not a Path");
    }
    File f = new File(getContext().getFilesDir() + File.separator + "solved" + path + ".jpg");
    int iMode;
    if ("r".equals(mode)) {
        iMode = ParcelFileDescriptor.MODE_READ_ONLY;
    } else if ("rw".equals(mode)) {
        iMode = ParcelFileDescriptor.MODE_READ_WRITE;
    } else if ("rwt".equals(mode)) {
        iMode = ParcelFileDescriptor.MODE_READ_WRITE | ParcelFileDescriptor.MODE_TRUNCATE;
    } else {
        throw new IllegalArgumentException("Invalid mode");
    }       
    return ParcelFileDescriptor.open(f, iMode); 
}

This works for every service I have installed with the ACTION_SEND intents except g+. Using your query method makes it work for google plus, too.

like image 165
user829996 Avatar answered Jan 10 '23 19:01

user829996