如何编写和读取具有自定义扩展名的文本文件? [英] How to write and Read a text file with custom extension?

查看:294
本文介绍了如何编写和读取具有自定义扩展名的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我正在使用 SAF .我要求用户输入名称,然后将一些数据保存到文件中.同样,我让用户从存储中选择文件并从中读取一些数据.我想使用我的自定义文件扩展名".xyz"来做到这一点.

In my app, I am creating a file and writing some data to it using SAF. I ask the user to enter the name and then save some data to the file. Similarly, I let the user chose the file from storage and read some data from it. I want to do this with my custom file extension say ".xyz".

我发现有多个使用Intent过滤器的资源,但是我对Intent过滤器的了解是,它们可以帮助您使用某些应用程序打开文件.单击文件后,我不想打开我的应用程序.我只想从应用程序内访问/读取文件.

I found multiple sources with intent-filters, but what I know about intent filters is that they help you open the file with some app. I don't want to open my app when the file is clicked. I just want to access/read the file from within the app.

当前可以保存扩展名为.xyz的文件.但无法阅读.我在意图过滤器中添加了以下代码

Presently when I am able to save the file with .xyz extension. But not able to read it. I have added following code in intent-filter

<data android:host="*" android:scheme="file" android:mimeType="text/plain" android:pathPattern=".*\\.xyz"/>

这是我读取文件的方式:

This is how I am reading the file:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/*");
startActivityForResult(intent, READ_REQUEST_CODE);

当文件浏览器打开以选择要读取的文件时,目标扩展名.xyz文件变得模糊.

When the file explorer opens for selecting files for reading, the target extension .xyz files come blurred.

推荐答案

text/*替换为*/*.

Android对自定义文件扩展名的作用很小,就像Web对自定义文件扩展名的作用很小.特别是,由于Android没有针对自定义文件扩展名的MIME类型,因此text/*将与之不匹配.

Android does very little with custom file extensions, just as the Web does very little with custom file extensions. In particular, since Android has no MIME type for a custom file extension, text/* will not match it.

这篇关于如何编写和读取具有自定义扩展名的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆