Android - Save image from URL onto SD card

后端 未结 7 1344
太阳男子
太阳男子 2020-11-28 07:37

I want to save an image from a URL to the SD card (for future use) and then load that image from the SD card to use it as a drawable overlay for Google maps.

Here is

7条回答
  •  没有蜡笔的小新
    2020-11-28 08:31

    DownloadManager does all these for you.

    public void downloadFile(String uRl) {
        File direct = new File(Environment.getExternalStorageDirectory()
                + "/AnhsirkDasarp");
    
        if (!direct.exists()) {
            direct.mkdirs();
        }
    
        DownloadManager mgr = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
    
        Uri downloadUri = Uri.parse(uRl);
        DownloadManager.Request request = new DownloadManager.Request(
                downloadUri);
    
        request.setAllowedNetworkTypes(
                DownloadManager.Request.NETWORK_WIFI
                        | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false).setTitle("Demo")
                .setDescription("Something useful. No, really.")
                .setDestinationInExternalPublicDir("/AnhsirkDasarpFiles", "fileName.jpg");
    
        mgr.enqueue(request);
    
        // Open Download Manager to view File progress
        Toast.makeText(getActivity(), "Downloading...",Toast.LENGTH_LONG).show();
        startActivity(Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
    
    }
    

提交回复
热议问题