Je travaille sur une application qui peut recevoir des fichiers PDF. L'application enregistre actuellement le fichier reçu sous la forme d'un octet [] dans le répertoire interne de l'application et on me donne alors accès à son chemin local.Android: Exporter un fichier PDF en utilisant le chemin de fichier
Je veux maintenant être en mesure de prendre ces données et de le convertir en un fichier PDF avant de l'enregistrer sur la mémoire externe.
Je suis capable de le faire en utilisant le code ci-dessous, mais quand j'essaie d'y accéder, on me dit qu'il est d'un format invalide. Une idée de comment réparer ça?
// ---------- 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);
}