I created an application to extract and download videos from pages with HTML players.
Well, the problem is that some downloads start to download but they just stop, well to try to find out the problem I plugged my cell phone into the computer, opened the android studio and went straight to the logcat, good first test a video like this , it usually opens and falls normally, however this video normally open but simply not low, I went to see the logcat to see if the download manager indicated some error but simply nothing, no error is indicated, before would like to make it clear that I do not understand much of HTTP headers, I thought the server should be denying the download by my app is not a "browser", so I added a User-Agent but it did not change anything, I used a site like this to see the headers of the two files to see if there was any field like "allow external downloads = false" but they were also the same. >
Well without further delay here is the code for the download manager:
package com.samuelives.videoplayer.system;
import android.app.DownloadManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v7.preference.PreferenceManager;
import android.util.Log;
import android.webkit.MimeTypeMap;
import com.samuelives.videoplayer.R;
import com.samuelives.videoplayer.util.StorageHelper;
import java.io.File;
public class Downloader {
private String mMimeType = "null", mExtension = "null";
private Context mContext = null;
private DownloadManager mManager = null;
public Downloader(Context context){
this.mContext = context;
mManager = (DownloadManager)mContext.getSystemService(Context.DOWNLOAD_SERVICE);
}
public void download(String title, Uri videoUri){
Log.i("Download Extension", getMimeType(videoUri.toString()));
DownloadManager.Request request = new DownloadManager.Request(videoUri);
request.setTitle(title);
request.addRequestHeader("User-Agent", getUserAgent());
if(mMimeType != "null"){
request.setMimeType(mMimeType);
}
if(isOnlyDownloadWithWifi()){
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
if(prefs.getBoolean("pref_key_is_storage_in_sdcard", true)){
if(StorageHelper.haveSdCard(mContext)){
File storageDir = StorageHelper.getStorage(mContext);
if(!storageDir.exists()){
storageDir.mkdirs();
}
Log.i("Download Path", storageDir.getAbsolutePath());
if(mMimeType != "null"){
request.setDestinationUri(Uri.fromFile(new File(storageDir.getAbsolutePath() + '/' + title + mExtension)));
}else{
request.setDestinationUri(Uri.fromFile(new File(storageDir.getAbsolutePath() + '/' + title + getMimeType(videoUri.toString()))));
}
}else{
if(mMimeType != "null") {
request.setDestinationUri( Uri.fromFile(new File(StorageHelper.getInternalStorage(mContext), title + mExtension)));
}else{
request.setDestinationUri( Uri.fromFile(new File(StorageHelper.getInternalStorage(mContext), title + getMimeType(videoUri.toString()))));
}
}
}else{
if(mMimeType != "null") {
request.setDestinationUri( Uri.fromFile(new File(StorageHelper.getInternalStorage(mContext), title + mExtension)));
}else{
request.setDestinationUri( Uri.fromFile(new File(StorageHelper.getInternalStorage(mContext), title + getMimeType(videoUri.toString()))));
}
}
mManager.enqueue(request);
}
//Set mimetype of download (optional)
public void setMimeTypeAndExtension(String mimeType){
this.mMimeType = mimeType;
if(mimeType != null){
this.mExtension = '.' + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
}
}
private String getMimeType(String uri){
String mime = '.' + MimeTypeMap.getFileExtensionFromUrl(uri);
if(mime.contains(".mp4") || mime.contains(".3gp") || mime.contains(".ogv")){
return mime;
}else{
return ".mp4";
}
}
private boolean isOnlyDownloadWithWifi(){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
return preferences.getBoolean("pref_key_is_download_only_wifi", true);
}
private String getUserAgent(){
boolean isTablet = mContext.getResources().getBoolean(R.bool.isTablet);
String ua = null;
if(isTablet){
ua = "Mozilla/5.0 (Linux; Android " + Build.VERSION.RELEASE + "; Tablet; rv: 41.0) Gecko/41.0 Firefox/41.0";
Log.d("User-Agent", ua);
}else{
ua = "Mozilla/5.0 (Linux; Android " + Build.VERSION.RELEASE + "; Mobile; rv: 41.0) Gecko/41.0 Firefox/41.0";
Log.d("User-Agent", ua);
}
return ua;
}
}
Here is my app if someone wants to do their own tests: link