[英]Trying to read Text File from Uri Android
I created an android file chooser that returns the uri of a text file.我创建了一个 android 文件选择器,它返回文本文件的 uri。 I want to open and read the file and store it's data.My code is:
我想打开并读取文件并存储它的数据。我的代码是:
private void covertFile(Uri data) {
InputStream inputStream = getContentResolver().openInputStream(data);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String myText = "";
int in;
try {
in = inputStream.read();
while (in != -1)
{
byteArrayOutputStream.write(in);
in = inputStream.read();
}
inputStream.close();
myText = byteArrayOutputStream.toString();
}catch (IOException e) {
e.printStackTrace();
}
myTextView.setText(myText);
}
But the line InputStream inputStream = getContentResolver().openInputStream(data);
但是行
InputStream inputStream = getContentResolver().openInputStream(data);
gives java.Io.FileNotFoundException
.给出
java.Io.FileNotFoundException
。 How do I resolve this?我该如何解决这个问题?
EDIT: Issue Resolved编辑:问题已解决
First create file object首先创建文件object
String path = data.toString();
File file = new File(path);
Now pass the file object as arg in InputStream现在将文件 object 作为 arg 传递到 InputStream
InputStream inputStream = getContentResolver().openInputStream(file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.