I am trying to create a button in my Android application, which allows the user to share the image using my choice of social networking networks. The image file is stored in the application resource folder.
My plan is to implement a custom ContentProvider to provide external access to the image, and then send the TYPE_SEND intent, which defines the image uri in my content provider.
I did this and it works for Google+ and GMail, but for other services it fails. The hardest part is finding information about what I should return from the query () method of my ContentProvider. Some applications specify a projection (for example, Google+ asks for _id and _data), while some applications skip zero as a projection. Even where the projection is indicated, I do not know what actual data (types) are expected in the columns. I can not find documentation on this.
I also implemented the openAssetFile ContentProvider method, and this is called (twice by Google+!), But then the request method is inevitably called. It seems that only the result of the query method is calculated.
Any ideas I'm wrong about? What should I return from my query method?
Code below:
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"));
public class ImageProvider extends ContentProvider
{
private AssetManager _assetManager;
public static final Uri CONTENT_URI = Uri.parse("content://com.me.provider");
@Override
public int delete(Uri arg0, String arg1, String[] arg2)
{
return 0;
}
@Override
public String getType(Uri uri)
{
return "image/jpeg";
}
@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
{
c.addRow(new Object[] { "ic_launcher.jpg", _assetManager.openFd("ic_launcher.jpg") });
} catch (IOException e)
{
e.printStackTrace();
return null;
}
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
{
return 0;
}
@Override
public String[] getStreamTypes(Uri uri, String mimeTypeFilter)
{
return new String[] { "image/jpeg" };
}
@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);
}
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException
{
return super.openFile(uri, mode);
}
}