- Android: Reliably share an image from assets via messaging, G , twitter, facebook?
- // 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);
- }