Java Android:使用文件路径导出PDF

Java Android:使用文件路径导出PDF,java,android,file,pdf,fileoutputstream,Java,Android,File,Pdf,Fileoutputstream,我正在开发一个可以接收PDF文件的应用程序。应用程序当前将收到的文件保存为内部应用程序目录中的字节[],然后授予我访问其localpath的权限 我现在希望能够在将数据保存到外部内存之前将其转换为PDF文件 我可以使用下面的代码来实现这一点,但是当我尝试访问它时,我被告知它的格式无效。有没有办法解决这个问题 // ---------- EXPORT IMAGE TASK ---------- private class ExportPDF extends AsyncTask<Void, V

我正在开发一个可以接收PDF文件的应用程序。应用程序当前将收到的文件保存为内部应用程序目录中的字节[],然后授予我访问其localpath的权限

我现在希望能够在将数据保存到外部内存之前将其转换为PDF文件

我可以使用下面的代码来实现这一点,但是当我尝试访问它时,我被告知它的格式无效。有没有办法解决这个问题

// ---------- EXPORT IMAGE TASK ----------
private class ExportPDF extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... voids) {
        String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
        File appDirectory = new File(pathToExternalStorage + "/" + getString(R.string.app_name));
        if (!appDirectory.exists()) {
            appDirectory.mkdirs();
        }

        File imageFile = new File(appDirectory.getAbsolutePath() + "/PDF_" + filename.hashCode() + ".pdf");
        if (!imageFile.exists()) {
            try {
                FileOutputStream fos = new FileOutputStream(imageFile.getPath());
                fos.write(new File(localPath).toString().getBytes());
                fos.close();
            } catch (FileNotFoundException e) {
                Log.e(TAG, e.toString());
            } catch (IOException e) {
                Log.e(TAG, e.toString());
            }
        }
        return imageFile.getAbsolutePath();
    }

    @Override
    protected void onPostExecute(String aString) {
        exportPDF(aString);
    }
}

private void exportPDF(String filePath) {
    Uri imageUri = Uri.parse(filePath);
    Intent sharingIntent = new Intent(Intent.ACTION_VIEW);
    sharingIntent.setDataAndType(imageUri, "application/pdf");
    startActivity(sharingIntent);
}
/------------导出图像任务----------
私有类ExportPDF扩展异步任务{
@凌驾
受保护的字符串背景(无效…无效){
字符串pathToExternalStorage=Environment.getExternalStorageDirectory().toString();
File appDirectory=new File(pathToExternalStorage+“/”+getString(R.string.app_name));
如果(!appDirectory.exists()){
appDirectory.mkdirs();
}
File imageFile=新文件(appDirectory.getAbsolutePath()+”/PDF_“+filename.hashCode()+”.PDF”);
如果(!imageFile.exists()){
试一试{
FileOutputStream fos=新的FileOutputStream(imageFile.getPath());
write(新文件(localPath).toString().getBytes());
fos.close();
}catch(filenotfounde异常){
Log.e(标记,e.toString());
}捕获(IOE异常){
Log.e(标记,e.toString());
}
}
返回imageFile.getAbsolutePath();
}
@凌驾
受保护的void onPostExecute(字符串aString){
出口PDF(阿斯汀);
}
}
私有void导出PDF(字符串文件路径){
Uri imageUri=Uri.parse(文件路径);
意向共享内容=新意向(意向.行动\视图);
sharingint.setDataAndType(imageUri,“application/pdf”);
startActivity(共享内容);
}
此代码的作用是一步一步:

  • 基于某个值创建一个
    文件
    对象(
    新文件(localPath)
  • 创建该文件路径的字符串表示形式(
    新文件(localPath).toString()
  • 创建该文件路径的字符串表示形式的
    字节[]
    新文件(localPath).toString().getBytes()
  • 字节[]
    写入
    文件输出流
结果是,
imageFile
标识的文件包含指向其他文件的路径。这不是有效的PDF

我猜您正在尝试将
localPath
的内容复制到
imageFile
,而这段代码没有这样做

一个更简单、更快、占用空间更少的解决方案是使用
FileProvider
直接从
localPath
向PDF查看器提供PDF,而不是制作数据的第二个副本

fos.write(new File(localPath).toString().getBytes());