如何将图像从Webview下载到Android中的自定义/私人文件夹中

问题描述 投票:1回答:1

我正在尝试从Webview的Android应用程序的自定义文件夹中下载图像。到目前为止,我只能使用以下代码下载Download文件夹中的文件。

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

是否可以将图像存储在其他文件夹(新创建的文件夹)中,而不是默认的Download文件夹中。实际上,我想在Android应用程序文件夹中下载图像,该文件夹将是私有的,用户无法通过文件管理器或其他应用程序访问。

我尝试使用以下代码创建文件夹,但是在/Android/data/com.abc.xyz/下创建文件夹,是否有任何方法可以直接在内部存储(在根目录下)创建文件夹?

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

if(!direct.exists()) {
     if(direct.mkdir());
}
android webview android-download-manager
1个回答
0
投票

尝试一下

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

    }
});
© www.soinside.com 2019 - 2024. All rights reserved.