Amir_P
Amir_P

Reputation: 9019

Can't send file correct URI using FileProvider

I'm creating a file with this path

File file = new File(getExternalFilesDir(null) + "/package.apk");

then I'm trying to send it using this code

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(FileProvider.getUriForFile(UpdateActivity.this, getPackageName() + ".provider", file), "application/vnd.android.package-archive");
Log.d("uri",FileProvider.getUriForFile(UpdateActivity.this, getPackageName() + ".provider", file).toString());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

my android manifest is

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/provider_paths" />
</provider>

and the provider_paths

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="files" path="." />
</paths>

After running code I'm getting There was a problem parsing the package error but if I open the file using file manager from path Android/my.package.name/files/package.apk it's parsing so the URI I'm sending is incorrect then I tried to print it and the result is

content://my.package.name.provider/files/package.apk

what am I doing wrong?

Upvotes: 1

Views: 610

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006554

You forgot to add FLAG_GRANT_READ_URI_PERMISSION on the Intent. As it stands, the Uri is valid, but the installer has no rights to access its content.

Also note that the stock Android installer only supports content schemes as of Android 7.0. Older Android devices are limited to file.

Upvotes: 1

Related Questions