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?