Android 7.1.1上的ActivityNotFoundException

时间:2017-04-26 08:19:40

标签: android android-7.1-nougat activitynotfoundexception

我有一个应用程序打开pdf文件,我们点击按钮。它在Android的所有版本上都是功能的,但它在Android 7.1.1上崩溃了,我不知道为什么:/

这些是我看过的相关问题

ActivityNotFoundException when starting

No Activity found to handle Intent splash screen

我在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();
}

我的清单:

<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>

最后是错误代码:

  

04-26 08:15:16.991 21748-21748 / com.example.fr E / AndroidRuntime:FATAL EXCEPTION:main                                                                                   处理:com.example.fr,PID:21748                                                                                   android.content.ActivityNotFoundException:找不到处理Intent的Activity {act = android.intent.action.VIEW dat = content://com.example.fr.fileprovider/assets/toto.pdf typ = application / pdf flg = 0x1}                                                                                       在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1809)                                                                                       在android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)                                                                                       在android.app.Activity.startActivityForResult(Activity.java:4225)                                                                                       在android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)                                                                                       在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)                                                                                       在android.app.Activity.startActivityForResult(Activity.java:4183)                                                                                       在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)                                                                                       在android.app.Activity.startActivity(Activity.java:4522)                                                                                       在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)                                                                                       在android.view.View.performClick(View.java:5637)                                                                                       在android.view.View $ PerformClick.run(View.java:22429)                                                                                       在android.os.Handler.handleCallback(Handler.java:751)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                       在android.os.Looper.loop(Looper.java:154)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

问题是您强制系统打开意图,而不检查是否有可以处理意图的应用程序。您可能正在尝试在没有应用程序读取PDF文件的设备上打开PDF。尝试使用此代码:

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

答案 1 :(得分:0)

  

它在所有Android版本上都是功能

不,事实并非如此。它在您测试的那些Android设备上运行,并且这些设备碰巧安装了支持content方案的PDF查看器。 Android本身没有PDF查看器,并且不要求所有设备都有PDF查看器,并且所有用户(多用户设备)都可以访问PDF查看器。

  

但它在Android 7.1.1上崩溃,我不知道为什么

您正在测试的设备没有支持content方案的PDF查看器。