0

想要使用 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错误。请帮我解决这个问题

4

1 回答 1

0

getFilesDir() 仅适用于您的应用程序的私有内部存储。您不能要求另一个应用程序将文件放在那里,因为它没有访问权限。

对于 DownloadManager 使用外部存储。

于 2017-04-15T09:05:59.417 回答