I have an problem . I try download a pdf file and save this file in my application folder. But when I try do this I have a error and file is not save...
This is my provider
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="Android/data/xxx/files/Download" />
</paths>
I put this in manifest
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="xxx.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths2" />
</provider>
I do this :
public static void parseBase64ToPDFAndNoOpen(String base64, String cardId ,Context context) throws IOException {
FileOutputStream os;
String path;
Uri photoURI = null;
try {
photoURI = FileProvider.getUriForFile(context.getApplicationContext(),
BuildConfig.APPLICATION_ID + ".provider", createImageFile(context,cardId));
} catch (IOException e) {
e.printStackTrace();
}
File dwldsPath = new File(photoURI.toString());
// File dwldsPath = new File(Environment.getExternalStorageDirectory() + "/" + File.separator + cardId + ".pdf");
if (!dwldsPath.exists()) {
dwldsPath.createNewFile();
byte[] pdfAsBytes = Base64.decode(base64, 0);
os = new FileOutputStream(dwldsPath, false);
os.write(pdfAsBytes);
os.flush();
os.close();
}
}
private static File createImageFile(Context context,String name) throws IOException {
File imagesDir = new File(getDefaultTempFilesPath(context));
return File.createTempFile(
name, /* prefix */
".pdf", /* suffix */
imagesDir /* directory */
);
}
private static String getDefaultTempFilesPath(Context context) {
if (context != null) {
File f = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if (f != null)
return f.getPath();
else {
f = context.getFilesDir();
if (f != null)
return f.getPath();
}
}
return null;
}
And I have : java.io.IOException: No such file or directory