我创建了一个Android文件选择器,该选择器返回文本文件的uri。我想打开并读取文件并存储其数据。我的代码是:
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);
}
但是线InputStream inputStream = getContentResolver().openInputStream(data);
给了java.Io.FileNotFoundException
。我该如何解决?
编辑:已解决的问题
首先创建文件对象
String path = data.toString();
File file = new File(path);
现在在InputStream中将文件对象作为arg传递
InputStream inputStream = getContentResolver().openInputStream(file);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句