java - 如何解决“未找到处理意图的活动”异常?
问题描述
我正在编写一个 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
例外。
解决方案
您使用了错误的文件类别(/)。它应该是类型而不是类别。
如果您想从存储中选择任何文件,您需要如下代码。
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();
}
}
来源:这里
推荐阅读
- java - Java:小键/值持久化器
- java - 在用于项目 Gridview 的自定义适配器中实现 Glide
- kendo-ui - 如何更改剑道条形图/折线图的路径填充颜色?
- c# - 有没有办法为 MenuItem ItemSource 的前 10 个项目分配 InputGestureText?
- node.js - 有没有办法通过 Graph API 检查是否已在应用程序上授予 requiredResourceAccess?
- cygwin - 如何修复 apt-cyg 中的“无法找到包”错误
- java - 如何在 Android Studio 中创建 Fuseki SPARQL 服务器连接
- javascript - 如何在组中使用 keydown 事件?
- ibm-datapower - 响应类型“通过”按预期工作,但是当我将其更改为“XML”时,只有 GET 请求有效
- java - 将对象写入 ObjectOutputStream 不起作用