Magic due to Logic
Magic due to Logic

Reputation: 121

How to download image from Webview to custom/private folder instead of Download folder in Android

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());
}

Upvotes: 1

Views: 855

Answers (1)

Majid Ali
Majid Ali

Reputation: 495

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);  

    }
});

Upvotes: 1

Related Questions