要从附件中读取数据,我使用:
Uri dataUri = getIntent().getData();
ContentResolver cr = getContentResolver();
String readXmlString = "";
try {
InputStream inputStream = cr.openInputStream(dataUri);
readXmlString = FileUtils.readFromInputStream(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
在大多数设备上,此代码运行正常,但在Kindle Fire上我遇到了问题。我调试了它。内容解析程序尝试打开输入流时会发生此问题。所以,我在readXmlString中有这样的东西: +v ( קi ^ %z z ʗ
我的readFromInputStream的代码:
public static String readFromInputStream(InputStream inputStream){
StringBuilder total = new StringBuilder();
try {
BufferedReader r = new BufferedReader(
new InputStreamReader(inputStream)); //also tried: new InputStreamReader(inputStream, "UTF-8")
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return total.toString();
}