1

I am trying to download image from webview in custom folder of Android app. As of now i can only download file in Download folder by using below code.

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imgUrl));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);

Is there any way to store image in other folder (newly created folder) instead of default Download folder. Actually i want to download image in Android app folder that will be private and not accessible to user via File Manager or other Apps.

I tried to create folder by using following code but folder is creating under /Android/data/com.abc.xyz/ is there any way to create folder directly under internal storage (under root directory) ?

File direct = new File(Environment.getExternalStorageDirectory()+"folder_name");

if(!direct.exists()) {
     if(direct.mkdir());
}

1 Answers1

1

try this

mywebView.setDownloadListener(new DownloadListener() {

    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

        Request request = new Request(Uri.parse(url));
        request.allowScanningByMediaScanner();

        request.setNotificationVisibility(
        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        File folder = new File("/storage/emulated/0/Folder_Name");
        if (!folder.exists()) {
        folder.mkdirs();
        System.out.println("Directory created");
        } else {
        System.out.println("Directory is not created");
        }
        request.setDestinationInExternalPublicDir("Folder_Name","Wallpaper.jpg");


        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

        dm.enqueue(request);  

    }
});
VikaS GuttE
  • 1,636
  • 2
  • 14
  • 38
Majid Ali
  • 485
  • 6
  • 17
  • How i can create new folder and store file in that folder? can i create private folder that can not accessible to user via File manager? Also could you please confirm in above code `download` is file name or Folder name? Sorry i am new to Android :( – Magic due to Logic Nov 14 '19 at 08:43
  • this is an another question! you can create ur own folder - do something like this https://stackoverflow.com/a/17795025/7924565 and dont forget to add permission in manifest. – Majid Ali Nov 14 '19 at 09:03
  • "download" is the name of file not folder. and directory_download is the download folder of device. – Majid Ali Nov 14 '19 at 09:11
  • Thank you so much for your help...! now only one issue is the folder is creating under `/Android/data/com.abc.xyz/` is there any way to create folder directly under root? – Magic due to Logic Nov 14 '19 at 10:32
  • try below code for `File folder = new File("/storage/emulated/0/Folder_Name"); if (!folder.exists()) { folder.mkdirs(); System.out.println("Directory created"); } else { System.out.println("Directory is not created"); } request.setDestinationInExternalPublicDir("Folder_Name", "Wallpaper.jpg");` – VikaS GuttE Nov 14 '19 at 11:18
  • try this File folder=new File(getFilesDir() , "foldername"); – Majid Ali Nov 14 '19 at 11:42
  • check if !exist then folder.mkdirs() – Majid Ali Nov 14 '19 at 11:44
  • Thank you so much @VikaS for your help...! – Magic due to Logic Nov 14 '19 at 11:57