PDFファイルを受信できるアプリケーションで作業しています。このアプリは、現在、受信したファイルを内部のアプリケーションディレクトリにbyte []として保存し、そのローカルパスにアクセスできます。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);
}