java - 在android中将PDF文件转换为字节数组
问题描述
我想在(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
解决方案
使用 java 7 中的包 java.nio.file
Path pdfFilePath = Paths.get("/file/path/your_file.pdf"); //File path
byte[] pdfByteArray = Files.readAllBytes(pdfFilePath );
推荐阅读
- java - 文件名中没有版本的 Maven 依赖项
- python - tf.data.Dataset 的一个热编码标签
- ios - 如何在 Swift 中将默认文本添加到 TableView
- macos - iTerm2 输入命令时添加额外字符
- python-3.x - 在产生输出的列旁边打印结果
- sql-server - SQL SERVER 中全局 SQL 临时表的条件重用
- c# - C#:OnRenderSplitButtonBackground 覆盖后消失的箭头
- python - 如何在此代码中的每个循环更新中防止“乌龟闪烁”?
- amazon-web-services - s3cmd 配置不接受远程路径
- java - 数组数组的 Jolt 变换规范