Android:使用隐式意图读取txt文件 [英] Android: Reading txt file using implicit intent
问题描述
问题:我正在尝试使用隐式Intent(ACTION_GET_CONTENT)
打开txt文件,并将txt文件的内容存储到arraylist中.当我尝试用Uri的getPath()
文件路径打开文件并创建一个BufferedReader
对象以从文本文件中读取时,出现错误消息说该文件路径不存在.
Problem : I'm trying to open a txt file using implicit Intent(ACTION_GET_CONTENT)
and store the txt file's content into an arraylist. When I try to open the file with file path from Uri's getPath()
and create a BufferedReader
object to read from the text file, I get an error says that such file path does not exist.
在Logcat中,它说我的文件路径是"/document/1505-2A0C:Download/text.txt"
当我尝试打开文件时说:
In Logcat, it says my file path is "/document/1505-2A0C:Download/text.txt"
and when I try to open the file it says:
"W/System.err: java.io.FileNotFoundException:
/document/1505-2A0C:Download/text.txt: open failed:
ENOENT (No such file or directory)"
这是我的代码:
@Override
public void onClick(View v) {
// Send implicit intent to load a file from directory
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
startActivityForResult(Intent.createChooser(intent,
"Load a file from directory"), REQUEST_CODE_SEARCH);
}
onActivityResult()
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_SEARCH) {
try {
Uri uri = data.getData();
String filepath = uri.getPath();
// In logcat : File path: /document/1505-2A0C:Download/text.txt
Log.d("File path", filepath);
File file = new File(filepath);
ArrayList<String> strings = new ArrayList<>();
/* Problem occurs here : I do not get correct file path to open a FileReader.
In logcat: "W/System.err: java.io.FileNotFoundException:
/document/1505-2A0C:Download/text.txt: open failed:
ENOENT (No such file or directory)"*/
BufferedReader br = new BufferedReader(new FileReader(file));
// Rest of code that converts txt file's content into arraylist
} catch (IOException e) {
// Codes that handles IOException
}
}
}
总结:我得到"/document/1505-2A0C:Download/text.txt"
的文件路径,当我使用文件路径在BufferedReader
中打开文件时,它说没有这样的目录.
In summary : I get "/document/1505-2A0C:Download/text.txt"
for file path, and when I open the file in BufferedReader
using the file path, it says there is no such directory.
我在这里做什么错了?
推荐答案
当我尝试使用Uri的getPath()中的文件路径打开文件并创建一个BufferedReader对象以从文本文件中读取时,我收到一条错误消息,指出该文件路径不存在.
When I try to open the file with file path from Uri's getPath() and create a BufferedReader object to read from the text file, I get an error says that such file path does not exist.
这是因为ACTION_GET_CONTENT
不返回文件.它返回Uri
,而Uri
不必指向文件.
That is because ACTION_GET_CONTENT
does not return a file. It returns a Uri
, and that Uri
does not have to point to a file.
清除所有File
内容.使用ContentResolver
和openInputStream()
在Uri
标识的内容上获取InputStream
.使用该InputStream
读入您的文本.
Get rid of all the File
stuff. Use a ContentResolver
and openInputStream()
to get an InputStream
on the content identified by the Uri
. Read in your text using that InputStream
.
这篇关于Android:使用隐式意图读取txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!