【发布时间】:2019-09-03 06:17:42
【问题描述】:
我想在 (onActivityResult) 中将 PDF 文件转换为字节数组 我尝试了几种不同的方法,但没有奏效 有知道的请回答。
更新:
case 1212 :
if (resultCode == RESULT_OK){
Uri uri = data.getData();
File file = new File(uri.getPath());
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(),"FileNotFound",Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"IOException",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}else Toast.makeText(getApplicationContext(),"خطا!",Toast.LENGTH_LONG).show();
此代码显示 FileNotFound Toast
【问题讨论】:
-
您为什么要这样做?如果您的 pdf 文件长度为 10 或 100 MB,该怎么办?
-
uri.getPath()通常不会返回文件路径。使用developer.android.com/reference/android/content/…从Uri打开流,然后读入字节数组。 -
but it didn't work究竟如何?