Search code examples
javaandroidandroid-studiofile-sharing

Getting URI instead of File when using FileProvider


I am creating an app in which I let the user to share an audio file that is presen on the external storage.

I try to use this code but it is not working.

            //This is getting the absolute path of the parent folder where the
            //file is situated 
            File mFolder= new File(m_sdcard+m_confi);

            // This take us to folder where the file is situated
            File mAbFolder = new File(mFolder, "temp");

            // Taking the files in an array
            File[] mAllFiles= mAbFolder.listFiles();

            //Getting the URI 
            Uri contentUri = FileProvider.getUriForFile(mContext, "com.androidpackagename.fileprovider", mImageFiles[1]);

            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setDataAndType(contentUri, mContext.getContentResolver().getType(contentUri));
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            mContext.startActivity(intent);

Now when I am running this code, it shows me the share menu. But when I click on any item such as Gmail, it put the uri into the "TO" field.

And I also tried to share it on Whatsapp. When I select a user to share this, nothing happens and I get back to my app.

My manifest is :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidpackagename">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.androidpackagename.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepath" />
    </provider>
</application>

</manifest>

My filepath xml is having

<external-path
    name="external"
    path="Android/data/com.androidpackagename/temp/" />
</paths>

Please help on this, thanks in advance.


Solution

  • I try to use this code but it is not working.

    That is not how you use ACTION_SEND. Quoting the documentation:

    Input: getType() is the MIME type of the data being sent. get*Extra can have either a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent. If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the MIME type of the data in EXTRA_STREAM.

    So, replace:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setDataAndType(contentUri, mContext.getContentResolver().getType(contentUri));
    

    with:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType(mContext.getContentResolver().getType(contentUri));
    intent.putExtra(Intent.EXTRA_STREAM, contentUri);
    

    (though it would be much more efficient for you to just put in the MIME type directly in setType(), rather than using getContentResolver().getType(contentUri), since hopefully you know what this file is and what its MIME type is)