I want to download a video file using DownloadManeger and then I want to save the video in Gallery using MediaStore for Android R. here is my code is below what I did. unfortunately, this code does not work for me. I need help
contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" "ThunderSave");
contentValues.put(MediaStore.Video.Media.TITLE, fileName);
contentValues.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
contentValues.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
contentValues.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
contentValues.put(MediaStore.Video.Media.IS_PENDING, 1);
ContentResolver resolver = this.getContentResolver();
Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri uriSavedVideo = resolver.insert(collection, contentValues);
try {
Uri uri = Uri.parse(VIDEO_URL);
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.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // This will show notification on top when downloading the file.
request.setTitle("Downloading data..."); // Title for notification.
request.setVisibleInDownloadsUi(true);
// request.addRequestHeader("Authorization", "bearer my bearertoken"));
// request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_MOVIES, fileName ".mp4");
request.setDestinationUri(uriSavedVideo);
((DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); // This will start downloading
} catch (Exception e) {
} ```
CodePudding user response:
You just need to pass your video URL to this downloadFile()
method
private void downloadFile(String vidurl) {
SimpleDateFormat sd = new SimpleDateFormat("yymmhh");
String date = sd.format(new Date());
String name = "video" date ".mp4";
try {
String rootFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
URL url = new URL(vidurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(rootFile,
name));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
}
f.close();
} catch (IOException e) {
Log.d("Error....", e.toString());
}
}
CodePudding user response:
request.setDestinationUri(uriSavedVideo)
Indeed, im not surprised it does not work.
DownloadManager is very picky about the type of uri you can use.
I found it not accepting uries from mediastore or storage access framework. Even file uries from a lot of locations or content schemes from FileProvider are not accepted.
But why using an uri?
You can just set destination directory to Movies and ThunderSave.
You can also ask DownloadManager to what you call 'add it to the gallery'.