【发布时间】:2015-09-13 04:39:03
【问题描述】:
我有一个 Uri 指向来自 intent 的文本文件,我正在尝试读取该文件以解析其中的字符串。这是我尝试过的,但使用FileNotFoundException 失败了。 toString() 方法似乎丢失了 /
java.io.FileNotFoundException: content:/com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7489a_711e4cf2/untitled%20. /p>
Uri data = getIntent().getData();
String text = data.toString();
if(data != null) {
try {
File f = new File(text);
FileInputStream is = new FileInputStream(f); // Fails on this line
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
text = new String(buffer);
Log.d("attachment: ", text);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
数据的值为:
content://com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7489a_711e4cf2/untitled%20text.txt
data.getPath() 的值为
/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7489a_711e4cf2/untitled text.txt
我现在正试图直接从 Uri 而不是路径获取文件:
Uri data = getIntent().getData();
String text = data.toString();
//...
File f = new File(text);
但 f 似乎丢失了 content://
中的一个斜线f:
内容:/com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7489a_711e4cf2/untitled%20text.txt
【问题讨论】:
-
您是否尝试过检查,我发现您的文件名中有空格 stackoverflow.com/questions/10301674/…。
-
@aleksamarkoni 我已经用 %20 而不是 " " 重新编码了 URL,但仍然是 FileNotFoundException
-
您能否在尝试的时候检查一下 SD 卡是否可用。 developer.android.com/training/basics/data-storage/…
-
使用
InputStream is = getContentResolver().openInputStream(uri)获取内容。见developer.android.com/reference/android/content/…
标签: java android file android-intent text