How can I download a video file to SD card?

后端 未结 3 1626
旧巷少年郎
旧巷少年郎 2020-12-01 09:59

I have a video file(.MP4 format) and I want to allow the user to be able to download the video to their SD card.I currently use this code but its not working..



        
3条回答
  •  我在风中等你
    2020-12-01 10:42

    First get Url from database then parse it in to URI and paste video uri in to DownloadManger constructor . I am retriving URL from firebase database with help to onComplete listiner.

     viewHolder.ll_download.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
    
                                    final String vdoUri = task.getResult().toString();
    
                                    try {
                                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(vdoUri));
                                        request.setDescription("download");
                                        request.setTitle(itemlist.get(position).getFile_name());
    
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                                            request.allowScanningByMediaScanner();
                                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                        }
                                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, ""+itemlist.get(position).getFile_name()+".mp4");
                                        DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                                        manager.enqueue(request);
                                    }
    
                                    catch (Exception ex) {
    
                                    }
    
    
                                }
                            });
    

提交回复
热议问题