Sharing an image in the application data directory via ContentProvider on Android

I am trying to open a .png file located in the application / data directory via ContentProvider, but instead of accessing the method openFile, it is being called query. Now I have only one image that I need to provide for sharing in other applications, how to set my intent to goto openFileinstead query?

Intent shareImageIntent = new Intent(Intent.ACTION_SEND);

            shareImageIntent.setType("image/*");

            shareImageIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            startActivity(Intent.createChooser(shareImageIntent, "Share image"));

Where Uri looks like

content://my.package.contentprovider/fileName

Or, conversely, do I need to create a database for this and return the cursor?

UPDATE

So, it looks like this works on everything except the SMS application (this is what I decided to test first), but I would like to support its sharing.

Here's the corresponding stack trace:

: java.lang.IllegalArgumentException: content://mypackage.myprovider/someImage.png . com.android.mms.ui.UriImage.initFromContentUri(UriImage.java:104) at com.android.mms.ui.UriImage. (UriImage.java:63) at com.android.mms.model.ImageModel.initModelFromUri(ImageModel.java:83) com.android.mms.model.ImageModel. (ImageModel.java:65) com.android.mms.data.WorkingMessage.changeMedia(WorkingMessage.java:481) com.android.mms.data.WorkingMessage.setAttachment(WorkingMessage.java:375)...

, SMS openFile, , , ( Google)

- , , , ? AOSP.

+5
3

SMS (MMS ) , .

UriImage.initFromContentUri 2 Cursor

 } else {
   filePath = c.getString(c.getColumnIndexOrThrow(Images.Media.DATA));
   mContentType = c.getString(c.getColumnIndexOrThrow(Images.Media.MIME_TYPE));
 }

, ContentProvider MMS-, Cursor query, (Images.Media.DATA Images.Media.MIME_TYPE ) . MMS openFile .

+4

(SD-), :

Uri imageUri = Uri.fromFile(pathToFile);

Update:

Uri imageUri = Uri.parse("android.resource://com.package.yourapp/" +imageResID);

Update2

Media Store, :

String url = Media.insertImage(context.getContentResolver(), imageFile.getAbsolutePath(), imageFile.getName(), imageFile.getName());

Uri imageUri = Uri.parse(url);

ContentProvider Cursor:

ContentProvider query(..), Cursor. . UrlImage.initFromContentUri(..) ( MMS-), , . MatrixCursor, .

+2

If your content provider is already running, you can access ParcelFileDescriptor using the openFileDescriptor method in the content provider.

A quick and dirty example for this:

ParcelFileDescriptor descriptor = mContext.getContentResolver().openFileDescriptor(IMGURI, "r");
Bitmap bmp = BitmapFactory.decodeFileDescriptor(descriptor.getFileDescriptor());

Hooray!

0
source

All Articles