0
我正在研究可以接收PDF文件的應用程序。該應用程序當前將接收到的文件保存爲內部應用程序目錄中的字節[],然後我可以訪問它的本地路徑。Android:使用文件路徑導出PDF
我現在希望能夠在將數據保存到外部存儲器之前將其轉換爲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);
}