簡體   English   中英

如何在Android中使用下載管理器在SD卡上保存文件?

[英]How to save file on SD card by using download manger in Android?

我在我的應用程序中面臨兩個問題。

1-我正在使用Android DownloadManager下載在線文件,但它將所有文件下載到系統目錄,例如使用Download目錄

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, "fileName");

現在我想將文件下載到我自己的目錄中說“新建文件夾”。 如何實施呢? 請在這方面幫助我,我會非常感激。

2-如何檢查SD卡是否可用?

使用這種方式:這里Dhaval_files是我的文件夾名稱

public void file_download(String uRl) {
        File direct = new File(Environment.getExternalStorageDirectory()
                + "/dhaval_files");

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

        DownloadManager mgr = (DownloadManager) this.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("/dhaval_files", "test.jpg");

        mgr.enqueue(request);

    }

下載管理器

1)

Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);
            request.setDescription("Downloading a file");
            long id =  downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(false)
                    .setTitle("File Downloading...")
                    .setDescription("Image File Download")
                    .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png"));

2)

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if(isSDPresent)
{
  // yes download code
}
else
{
 // No sdcard 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM