簡體   English   中英

使用下載管理器將媒體存儲在內部存儲中

[英]Store media in internal storage using download manager

想要使用Uri將音樂文件存儲在getFilesDir()中。 我曾嘗試過用這種方式存儲

Uri Download_Uri = Uri.parse(songBean.vSongsFileName);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle(songBean.vTitle);
request.setDescription("Downloading File");

    try {
    request.setDestinationUri(Uri.parse(createDirectory("tmp.mp3").getPath()));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    downloadReference = mDownloadManager.enqueue(request);
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

        }
    }, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

內部存儲器中創建目錄的方法

public File createDirectory(String filename) throws FileNotFoundException {
    File file = new File(context.getFilesDir(), filename);
    Log.i("Tag", "createDirectory: " + file.getAbsolutePath());
    return file;
}

無法將文件存儲在內部存儲器中。 request.setDestinationUri(Uri.parse(createDirectory("tmp.mp3").getPath())); not a file uri拋出not a file uri錯誤。為此請幫助我

getFilesDir()是僅用於您的應用程序的私有內部存儲。 您無法要求其他應用將文件放在那里,因為它沒有訪問權限。

對於DownloadManager,請使用外部存儲。

暫無
暫無

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

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