下载失败-kotlin

fwzugrvs  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(142)

我是Kotlin的新手,我正在创建一个项目。我的任务是下载音频(mp3格式)。我没有得到任何错误,在logcat,但当试图下载音频得到“下载不成功”。这是我的异步任务文件:

class DownloadAudioFromUrl(val context: Context, val audio: Audio) {
    @RequiresApi(Build.VERSION_CODES.O)
    fun download() {
        try {
            val audioName = audio.title + LocalDateTime.now() + ".mp3"
            val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
            val uri = Uri.parse(audio.filePath)
            val request = DownloadManager.Request(uri)
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
                .setTitle(audio.title)
                .setDescription("Downloading: " + audio.title)
                .setAllowedOverMetered(true)
                .setAllowedOverRoaming(true)
                .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, audioName)
            downloadManager.enqueue(request)
        } catch (e: Exception) {
            println("Error downloading: $e")
        }
    }
}

curplayingaudio不为空。这是我的名单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.daniil_holdings.soundy">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    <application
        android:usesCleartextTraffic="true"
        android:name=".SoundyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar">
    <service android:name=".exoplayer.MusicService">
        <intent-filter>
            <action android:name="android.media.browse.MediaBrowserService" />
        </intent-filter>
    </service>
    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/res"/>
    </provider>
    <activity android:name=".ui.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>
</manifest>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题