首页 > 解决方案 > 如何将 mp3 文件从 url 下载到内部存储?

问题描述

我做了一个下载器,它可以工作,但问题是当我从手机下载时没有外部存储它不起作用

这是我的代码,它适用于外部存储:-

lateinit var downloadManager :DownloadManager
lateinit var request : DownloadManager.Request
downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

btndownload.setOnClickLisner{
request = DownloadManager.Request(Download_Uri)
request.setAllowedOverRoaming(false)
request.setTitle(songdownloadedname)
request.setDescription(null)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, songdownloadedname)
request.setVisibleInDownloadsUi(true)
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
downloadManager.enqueue(request)

}

我可以编辑什么以将其下载到内部存储而不是外部存储?

标签: javaandroidandroid-studiokotlin

解决方案


您可以使用DownloadManager下载文件。你可以使用这个博客(不是我的博客)作为参考。以下源代码取自博客:

public class DownloadManagerActivity extends Activity {
    private long enqueue;
    private DownloadManager dm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {

                            ImageView view = (ImageView) findViewById(R.id.imageView1);
                            String uriString = c
                                    .getString(c
                                            .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                            view.setImageURI(Uri.parse(uriString));
                        }
                    }
                }
            }
        };

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

    public void onClick(View view) {
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        Request request = new Request(
                Uri.parse("your mp3 url"));
        enqueue = dm.enqueue(request);

    }

    public void showDownload(View view) {
        Intent i = new Intent();
        i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
        startActivity(i);
    }
}

推荐阅读