在Android应用开发中,实现接收其他应用共享的文本文件内容是一项实用功能,比如用户从文件管理器选择文本文件分享到你的应用,或者从其他支持分享的应用传递文本内容,都需要对应的处理逻辑。

功能实现前置准备
首先需要在AndroidManifest.xml中配置对应的Activity,声明该Activity可以接收文本类型的共享文件,具体配置如下:
<activity
android:name=".ReceiveTextFileActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
上面的配置中,android.intent.action.SEND用于接收单个共享文件,android.intent.action.SEND_MULTIPLE用于接收多个共享文件,text/*表示接收所有文本类型的文件。
解析传入的Intent数据
在接收共享文件的Activity中,首先需要在onCreate方法中获取传入的Intent,判断Intent的类型和携带的数据:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_text_file);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null && type.startsWith("text/")) {
// 处理单个共享文本文件
handleSingleSend(intent);
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null && type.startsWith("text/")) {
// 处理多个共享文本文件
handleMultipleSend(intent);
} else {
// 非共享文件场景,做默认处理
finish();
}
}
读取单个共享文本文件内容
当接收到单个共享文本文件时,Intent会携带一个Uri类型的数据,通过ContentResolver可以读取该Uri对应的文件内容:
private void handleSingleSend(Intent intent) {
Uri fileUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (fileUri != null) {
String content = readTextFromUri(fileUri);
// 处理读取到的文本内容
Log.d("ReceiveTextFile", "读取到的文本内容:" + content);
TextView contentTv = findViewById(R.id.content_tv);
contentTv.setText(content);
}
}
private String readTextFromUri(Uri uri) {
StringBuilder stringBuilder = new StringBuilder();
try {
// 通过ContentResolver打开文件输入流
InputStream inputStream = getContentResolver().openInputStream(uri);
if (inputStream != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append("n");
}
reader.close();
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
return "读取文件失败:" + e.getMessage();
}
return stringBuilder.toString();
}
读取多个共享文本文件内容
如果接收到多个共享文本文件,Intent会携带一个Uri的列表,需要遍历列表逐个读取文件内容:
private void handleMultipleSend(Intent intent) {
ArrayList<Uri> fileUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (fileUris != null && !fileUris.isEmpty()) {
StringBuilder allContent = new StringBuilder();
for (int i = 0; i < fileUris.size(); i++) {
Uri uri = fileUris.get(i);
String singleContent = readTextFromUri(uri);
allContent.append("第").append(i + 1).append("个文件内容:n");
allContent.append(singleContent);
allContent.append("n--------------------n");
}
// 处理所有文件的内容
TextView contentTv = findViewById(R.id.content_tv);
contentTv.setText(allContent.toString());
}
}
注意事项
- Android 6.0及以上版本,虽然读取共享文件不需要额外申请存储权限,但如果你的应用需要保存读取到的内容到本地,还是需要申请对应的存储权限。
- 读取文件时建议使用try-catch包裹,避免文件不存在或者读取异常导致应用崩溃。
- 如果共享的文件不是文本类型,即使配置了text/*的过滤,也可能接收到其他类型文件,需要在读取前做类型校验。
- 测试时可以从系统的文件管理器选择一个txt文件,点击分享选择你的应用进行测试。
常见问题解答
为什么配置后无法接收到共享文件
首先检查AndroidManifest中Activity的exported属性是否设置为true,其次检查intent-filter的action和data配置是否正确,最后确认分享的文件类型是否是text/*类型。
读取大文件时出现内存问题怎么办
如果文本文件过大,不建议一次性读取全部内容到内存,可以采用分段读取的方式,或者只读取文件的前N行内容展示。
实现Android接收共享文本文件内容的核心是通过Intent过滤接收共享事件,再通过ContentResolver读取Uri对应的文件内容,按照上述步骤即可完成功能开发。
Android共享文本文件IntentContentResolver文件读取修改时间:2026-07-15 20:45:28