1

My app uses a downloadFile Class to download an image and save in into /DCIM/Camera Folder, Then after downloading the file it sets it last modified time.

This is the FileDownloader class i use:

class FileDownloader extends AsyncTask<String, String, String> {
private String pathFolder = "";
String pathFile = "";
Context ctxt;
public FileDownloader(Context ctxt)
{
    this.ctxt=ctxt;
}
@Override
protected String doInBackground(String... f_url) {
    int count;
    try {
        pathFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/";
        pathFile = pathFolder + "/"+f_url[1];
        appendLog("FileDownloader.Downloading..="+f_url[0]);
        final File file = new File(pathFile);
            if(file.exists()){
                try {
                    file.delete();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        URL url = new URL(f_url[0]);
        URLConnection connection = url.openConnection();
        connection.connect();

        InputStream input = new BufferedInputStream(url.openStream());
        FileOutputStream output = new FileOutputStream(file);
        byte data[] = new byte[1024];
        while ((count = input.read(data)) != -1) {
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        input.close();
        try {
            long time= System.currentTimeMillis()-(86400000*Integer.parseInt(f_url[2]));
                file.setLastModified(time);
                //Scan Media File
                Intent mediaScannerIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                File f=new File(pathFile);
                Uri contentUri=Uri.fromFile(f);
                mediaScannerIntent.setData(contentUri);
                ctxt.sendBroadcast(mediaScannerIntent);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        Log.e("Error: ", e.getMessage());
    }
    return pathFile;
}

}

I use this line to download the image

new FileDownloader(getApplicationContext()).execute("https://test.in/test/test.php?n=4","Test.jpg","5");

This code works fine in devices upto android version Pie but doesn't work in Q. No file is created in Android 10. I used downloadManager before to download the image file but in some devices downloadManager was showing a dialog to confirm the download, so i used this method, but it doesn't work in android 10.

How can i download the file in camera folder and set its lastModifiedTime in android 10?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 3
    I'm guessing it's because you're still using _Environment.getExternalStoragePublicDirectory_ that is deprecated in 10. Consider using _getExternalFilesDir()_ instead or implementing _storage access framework_ or _Media Store_. This is a similar answer with the appropriate links: https://stackoverflow.com/a/58430303/10300673 – Nikos Hidalgo May 20 '20 at 07:06
  • Please dont download to DCIM/Camera folder. That is for the Camera app. – blackapps May 20 '20 at 07:15
  • See also a long series of blog posts that I wrote on scoped storage, starting with [this one](https://commonsware.com/blog/2019/10/19/scoped-storage-stories-saf-basics.html). – CommonsWare May 20 '20 at 11:00

0 Answers0