【问题标题】:I'm trying to download the audio using DownloadManager in Android Studio but it's downloading an unknown file我正在尝试使用 Android Studio 中的 DownloadManager 下载音频,但它正在下载未知文件
【发布时间】:2021-12-30 19:31:03
【问题描述】:

我有一个链接。 如果我直接使用 DownloadManager 下载此链接,它会发回一个未知文件。
但是当我在网上运行这个链接时,它显示下载为 mp3。 running this link on the web
我想直接下载音频而不运行到 WebView。

感谢您的帮助!

【问题讨论】:

  • 它看起来像一个流文件。您需要做的是在 Android 中找到一个 Stream-Downloader。

标签: android android-download-manager


【解决方案1】:

试试这个

添加以下库...

implementation 'com.github.esafirm:rxdownloader:2.0.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.10'

现在调用下面的函数

public static void download(Context context, String url, String name){
    RxDownloader rxDownloader = new RxDownloader(context);

    Uri uri = Uri.parse(url); // Path where you want to download file.
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);  // Tell on which network you want to download file.
    request.setTitle(name + ""); // Title for notification.
    request.setVisibleInDownloadsUi(true);
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);  // This will show notification on top when downloading the file.

    File file=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+YOUR_FOLDER_NAME)
    if(!file.exists()) file.mkdris(); //make sure you have storage permission


    request.setDestinationInExternalPublicDir(DIRECTORY_MUSIC, YOUR_FOLDER_NAME + "/" + name);

    //rxDownloader.download(request).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new CoreMySave1(Core.this, this, file2.getAbsolutePath(), isOnlyAudio));
    rxDownloader.download(request).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
        @Override
        public void onSubscribe(@NonNull Disposable d) {
            Log.d("====", "onSubscribe");

        }

        @Override
        public void onNext(@NonNull String s) {

        }

        @Override
        public void onError(@NonNull Throwable e) {
            e.printStackTrace();
            Log.d("====", "onError :: " + e.getMessage());

        }

        @Override
        public void onComplete() {
            Log.d("====", "onComplete");

        }
    });
}

【讨论】:

  • 感谢您的帮助,但它不起作用:(
  • 有什么问题?请确认您有存储访问权限,并且音乐目录中有可用的文件夹。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 2022-09-28
  • 2012-05-03
  • 2020-05-31
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多