1

I'm working on backup and restore SQLite Database to cloud server. I've completed the backup code and it works. However, I have a problem when I'm trying to restore it. The problem is, on Android 11, the Environment.DIRECTORY_DOWNLOADS is in Android/data/package/files/Download, but somehow I cannot access or write to Environment.DIRECTORY_DOWNLOADS, and I don't know why. Here's my code (the download from cloud method, it succeeds).

DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(response.body().getPath());
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, sharedPreference.getUser().getEmail() + ".db");
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setTitle(sharedPreference.getUser().getEmail() + ".db");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
Long downloadReference = manager.enqueue(request);

When I'm trying to access them, I cannot access the Environment.DIRECTORY_DOWNLOADS. Here is the code

File internal = new File(Environment.DIRECTORY_DOWNLOADS);

if (internal.canRead()) {
    File currentDB = new File("/data/data/" + getPackageName() + "/databases/", DBHelper.DATABASE_NAME);
    File backupDB = new File(internal, sharedPreference.getUser().getEmail() + ".db");
    Toast.makeText(getApplicationContext(),backupDB.toString(),Toast.LENGTH_SHORT).show();

    if (backupDB.exists()) {
        FileChannel src = new FileInputStream(backupDB).getChannel();
        FileChannel dst = new FileOutputStream(currentDB).getChannel();
        dst.transferFrom(src, 0, src.size());
        src.close();
        dst.close();
        progressDialog.dismiss();
        Toast.makeText(getApplicationContext(), backupDB.toString(), Toast.LENGTH_SHORT).show();
    } else {
        progressDialog.dismiss();
        Toast.makeText(getApplicationContext(), "Error, file not exist!", Toast.LENGTH_SHORT).show();
    }

} else {
    progressDialog.dismiss();
    Toast.makeText(getApplicationContext(), "Error, cannot read the directory!", Toast.LENGTH_SHORT).show();
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rio
  • 11
  • 1
  • `DownloadManager` is not useful for your scenario. It is now only for downloading content where the *user* needs access to the content, but the *app* does not need access to the content. In your case, you should download the content yourself, such as by using OkHttp: https://stackoverflow.com/a/29012988/115145 – CommonsWare Aug 14 '21 at 10:41
  • `The problem is, on Android 11, the Environment.DIRECTORY_DOWNLOADS is in Android/data/package/files/Download, ` No not at all. Environment.DIRECTORY_DOWNLOADS is just the string "Download". You can use that string where you want. – blackapps Aug 14 '21 at 11:10
  • `but somehow i cannot access or write to Environment.DIRECTORY_DOWNLOADS,` Of course not as that is just the name Download. But you have full access to `request.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, ` . Try getExternalFilesDir(Environment.DIRECTOEY_DOWNLOADS) – blackapps Aug 14 '21 at 11:15
  • What are your targetSDK and compileSDK numbers? – Amar Ilindra Aug 14 '21 at 11:51

0 Answers0