Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 2.19 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android: Reliably share an image from assets via messaging, G , twitter, facebook?
  2. // my intent
  3.  
  4. Intent i = new Intent(android.content.Intent.ACTION_SEND);
  5. i.setType("image/jpeg");
  6. Uri uri = Uri.parse("content://com.me.provider/ic_launcher.jpg");
  7. i.putExtra(Intent.EXTRA_STREAM, uri);      
  8. i.putExtra(android.content.Intent.EXTRA_TEXT, text);
  9. startActivity(Intent.createChooser(i, "Share via"));
  10.  
  11. // my custom content provider
  12.  
  13. public class ImageProvider extends ContentProvider
  14. {
  15. private AssetManager _assetManager;
  16.  
  17. public static final Uri CONTENT_URI = Uri.parse("content://com.me.provider");
  18.  
  19. // not called
  20. @Override
  21. public int delete(Uri arg0, String arg1, String[] arg2)
  22. {
  23.     return 0;
  24. }
  25.  
  26. // not called
  27. @Override
  28. public String getType(Uri uri)
  29. {
  30.     return "image/jpeg";
  31. }
  32.  
  33. // not called
  34. @Override
  35. public Uri insert(Uri uri, ContentValues values)
  36. {
  37.     return null;
  38. }
  39.  
  40. @Override
  41. public boolean onCreate()
  42. {
  43.     _assetManager = getContext().getAssets();
  44.     return true;
  45. }
  46.  
  47. @Override
  48. public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
  49. {
  50.     MatrixCursor c = new MatrixCursor(new String[] { "_id", "_data" });
  51.  
  52.     try
  53.     {
  54.                     // just a guess!! works for g+ :/
  55.         c.addRow(new Object[] { "ic_launcher.jpg",  _assetManager.openFd("ic_launcher.jpg") });
  56.     } catch (IOException e)
  57.     {
  58.         e.printStackTrace();
  59.         return null;
  60.     }
  61.  
  62.     return c;
  63. }
  64.  
  65. // not called
  66. @Override
  67. public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
  68. {
  69.     return 0;
  70. }
  71.  
  72. // not called  
  73. @Override
  74. public String[] getStreamTypes(Uri uri, String mimeTypeFilter)
  75. {
  76.  
  77.     return new String[] { "image/jpeg" };
  78. }
  79.  
  80. // called by most apps
  81. @Override
  82. public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException
  83. {
  84.  
  85.     try
  86.     {
  87.         AssetFileDescriptor afd = _assetManager.openFd("ic_launcher.jpg");
  88.         return afd;
  89.     } catch (IOException e)
  90.     {
  91.         throw new FileNotFoundException("No asset found: " + uri);
  92.     }
  93. }
  94.  
  95. // not called
  96. @Override
  97. public ParcelFileDescriptor openFile(Uri uri, String mode)
  98.         throws FileNotFoundException
  99. {
  100.  
  101.     return super.openFile(uri, mode);
  102. }