Narmi
Narmi

Reputation: 48

ActivityNotFoundException on Android 7.1.1

I have an application which open a pdf file whan we click on button. It was functionnal on all versions of Android but it crash on Android 7.1.1 and i don't know why :/

These are the related questions I have looked at

ActivityNotFoundException when starting

No Activity found to handle Intent splash screen

My function to open file in MainActivity:

private void readPDF({
    File f = new File(getFilesDir(), "toto.pdf");

    if (!f.exists()) {
        AssetManager assets=getResources().getAssets();

        try {
            copy(assets.open("toto.pdf"), f);
        }
        catch (IOException e) {
            Log.e("FileProvider", "Exception copying from assets", e);
        }
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = getUriForFile(this, getApplicationContext().getPackageName() + ".fileprovider", f);
    intent.setDataAndType(uri, "application/pdf");
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    revokeUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
}

private void copy(InputStream in, File dst) throws IOException {
    FileOutputStream out=new FileOutputStream(dst);
    byte[] buf=new byte[1024];
    int len;

    while ((len=in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    in.close();
    out.close();
}

My manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fr">
<application
    android:allowBackup="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

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

    <activity
        android:name="com.example.fr.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Finally the error code :

04-26 08:15:16.991 21748-21748/com.example.fr E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fr, PID: 21748 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.example.fr.fileprovider/assets/toto.pdf typ=application/pdf flg=0x1 } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1809) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523) at android.app.Activity.startActivityForResult(Activity.java:4225) at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75) at android.app.Activity.startActivityForResult(Activity.java:4183) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871) at android.app.Activity.startActivity(Activity.java:4522) at android.app.Activity.startActivity(Activity.java:4490) at com.example.fr.MainActivity.readPDF(MainActivity.java:58) at com.example.fr.MainActivity.access$000(MainActivity.java:21) at com.example.fr.MainActivity$1.onClick(MainActivity.java:34) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Thank's for your help

Upvotes: 0

Views: 1671

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006539

It was functionnal on all versions of Android

No, it was not. It was functional on those Android devices that you tested, and those devices happened to have a PDF viewer installed that supported the content scheme. Android itself does not have a PDF viewer, and there is no requirement that all devices have a PDF viewer and all users (of multi-user devices) have access to a PDF viewer.

but it crash on Android 7.1.1 and i don't know why

The device that you are testing on does not have a PDF viewer that supports the content scheme.

Upvotes: 0

Luca Nicoletti
Luca Nicoletti

Reputation: 2397

The problem is that you force the system to open the intent, without checking if there's an application that can handle the intent. Probably you're trying to open the PDF on a device that has not an application for reading a PDF file. Try using this code:

PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent);
} else {
    Log.d(TAG, "No Intent available to handle action");
}

Upvotes: 1

Related Questions