Image not being shared to Apps

I want my users to be able to share an image and select an app to share it to whether its something like their native messenger app, or twitter. Whenever I go to select the app I want to share the image to, I get a message saying “This media can’t be loaded” or something like that. Here is the sharing code in BitmapUtils.java

static void shareImage(Context context, String imagePath) {
    // Create the share intent and start the share activity
    File imageFile = new File(imagePath);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    Uri photoURI = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, imageFile);
    shareIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
    context.startActivity(shareIntent);
}

Here is my file provider code in my Manifest file:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.aaronapp.hideme.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />

Here is the file_paths file which contains the file paths.

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-cache-path name="my_cache" path="." />
    <external-path name="my_images" path="Pictures/" />
</paths>

Here is the share method that is invoked inside the MainActivity class.

/**
 * OnClick method for the share button, saves and shares the new bitmap.
 */
@OnClick(R.id.share_button)
public void shareMe() {
    // Delete the temporary image file
    BitmapUtils.deleteImageFile(this, mTempPhotoPath);

    // Share the image
    BitmapUtils.shareImage(this, mTempPhotoPath);
}

If you need anymore information that I forgot to show I’ll be happy to supply it. I’m trying to fix this issue and get my images to share to different apps(I know Facebook has a certain way of sharing images, but I will tackle that later)

You can also replicate this issue by downloading the Hide me, Emoji App on the google play store, taking a picture and trying to share it across your apps.https://play.google.com/store/apps/details?id=com.aaronapp.hideme

Answer

This will allow you to read files from Internal or external sdcard.

Add this in manifest.xml

<!-- this is For Access External file Storage -->
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.demo.test.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"/>
</provider>

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />
    <root-path
        name="external_files"
        path="/storage/"/>
</paths>

Leave a Reply

Your email address will not be published. Required fields are marked *