首页 > 解决方案 > 如何解决“未找到处理意图的活动”异常?

问题描述

我正在编写一个 Android FTP 服务器程序。我需要选择要发送给客户端的文件,所以我需要一个文件选择器,我编写了以下代码:

爪哇:

Intent filechooser= new Intent(Intent.ACTION_GET_CONTENT);

    filechooser.addCategory("*/*");
    filechooser.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    startActivityForResult(filechooser, 10);

XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sender">
<uses-permission android:name="android.permission.INTERNET" />
<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:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Chooser"></activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

我得到一个No activity found to handle intent例外。

标签: javaandroiduser-interfaceandroid-activity

解决方案


您使用了错误的文件类别(/)。它应该是类型而不是类别。

如果您想从存储中选择任何文件,您需要如下代码。

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("*/*"); 
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.", 
                Toast.LENGTH_SHORT).show();
    }
}

来源:这里


推荐阅读