Android: download arrow doesn't appear - android

i'm trying to develop an app that shows video and you can download it on your mobile
the app is working perfectly, but my problem is that the download arrow doesn't appear in notification bar.
I'm using download manager class.
Here's my method:
public void downloadFileFromUrl(String url, String fileName, DownloadManager downloadManager) {
String filePath=Environment.getExternalStorageDirectory() + File.separator + "BlueNet";
try {
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setDestinationInExternalPublicDir("/BlueNet",fileName);
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
request.allowScanningByMediaScanner();
idDwnldMng= downloadManager.enqueue(request);
}
catch (Exception ex){
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
Can someone help me?

Seems like you missing the permission:
<uses-permission
android:name="WRITE_EXTERNAL_STORAGE" />

Related

DownloadManager throws SecurityException on Android Q

Download Manager gets the error below on Android 10 devices. The target version is 29.
I added android:requestLegacyExternalStorage="true" tag to the Manifest, but it didn't work.
java.lang.SecurityException: Unsupported path /storage/emulated/0/Contents/Awesome App.apk
Here is the code
public static void startDownload(Context context, String url, String token, String subPath) {
DownloadManager.Request request;
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url); // A url to download a file
try {
request = new DownloadManager.Request(uri);
request.addRequestHeader("X-Auth-Token", token);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
}
request.setVisibleInDownloadsUi(true);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
try {
File downloadFileDir = new File(Environment
.getExternalStorageDirectory().getAbsolutePath() + "/Contents");
if (downloadFileDir != null) {
if (!downloadFileDir.exists()) {
downloadFileDir.mkdirs();
}
File file = new File(downloadFileDir.getAbsolutePath() + File.separator + subPath);
// subPath is name of the file to download. e.g. Awesome App.apk
if (file.exists()) {
file.delete();
}
Uri localUri = Uri.fromFile(file);
request.setDestinationUri(localUri);
if (localUri != null) {
request.setMimeType(MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(localUri.toString())));
}
}
} catch (SecurityException e) {
e.printStackTrace();
}
request.setTitle(subPath);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
try {
manager.enqueue(request);
} catch (SecurityException e) {
e.printStackTrace();
//Got exception here
}
}
/storage/emulated/0/Contents/Awesome App.apk
In an Android 10 device the DownloadManager will not download to your own directories on external storage.
You need to use one of the already available public directories like Document, Download, DCIM, Music and so on.
So you can let download to
/storage/emulated/0/Music/Contents/Awesome App.apk
No need to create your subdirectory yourself as the download manager will do it.
You app does not need any permission to let the download manager execute its task.

File Not Getting Download Within WebView

I am working with an android application where i need to open a form URL in my webview. I am able to open the form in webview, there is a submit button with form, click on this button will download the file within webview but its not happening. I am able to download the file with normal browser on mobile like google chrome. How can i download the file within my application webview.
i don't know how the you are trying, but you can use this code to download the file inside the webview
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
and add this permission to your android manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
hope it will help.!

Download Manager not working

I'm trying to develop app that show videos and you can Download it
i'm using Download Manager class but it didn't work, also it didn't give me any error :(
this is my download manager code:
public void downloadFileFromUrl(String url, String fileName) {
String filePath=Environment.getExternalStorageDirectory() + File.separator + "BlueNet";
File folder = new File(filePath);
if (!folder.exists()) {
folder.mkdirs();
}
try {
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.allowScanningByMediaScanner();
request.setDestinationInExternalPublicDir("/BlueNet/",fileName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
DownloadManager downloadManager = (DownloadManager)getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
long id= downloadManager.enqueue(request);
Toast.makeText(this, fileName, Toast.LENGTH_LONG).show();
Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();
}
catch (Exception ex){
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
and this is how I'm calling it
downloadFileFromUrl(path, fileName);
where:
path: "192.168.1.5:8080/BlueNet_NMC/blue_elephant.mp4"
filename: "blue_elephant.mp4"
and i already give this permissions to manifests
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
so please any help
As I said in the comments, DownloadManager only handles requests starting with http:// or https:// as you can see in the docs.
I don't know exactly what's the problem because I lack information about your server, but I think it's a common issue, so you should avoid using an IP address without providing that scheme.
I had a problem when downloading files with an HTTP URL using the DownloadManger class; but then I did the following and the problem was fixed.
Instead of this code:
String url = "http://masteranime.ir/music/best/Dragon Ball GT Dan Dan Kokoro Hikareteku.mp3";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
use this code:
String url = "http://masteranime.ir/music/best/Dragon Ball GT Dan Dan Kokoro Hikareteku.mp3";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url).replaceAll(" ","%20"));
You have problem in this line - request.setDestinationInExternalPublicDir("/BlueNet/",fileName);
Just remove this line or make directory in another way.
request.setDestinationInExternalPublicDir("/BlueNet/", fileName);
You have to mention directory as first argument here. /BlueNet/ is not a directory.

How to avoid API level warning?

I'm trying to make a method to download a file, but inside the method in the request, it has a call to a function that show a notification of the download. The problem is that the minimum api required for my app is API 9, and the IDE shows me an error because setNotificationVisibility is for API 11 and above.
This is my source code:
public void init_download(String title, String filename, String url) {
File path = new File(Environment.DIRECTORY_DOWNLOADS);
if (!path.exists()) {
path.mkdirs();
}
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(title)
.setDescription(getResources().getString(R.string.downloading))
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
long id = downloadManager.enqueue(request);
//Save the request id
SharedPreferences.Editor PrefEdit = shared_pref.edit();
PrefEdit.putLong("DOWNLOAD_ID", id);
PrefEdit.commit();
}
How can I solve this to run notifications in both versions?
Thank you.
Add #SuppressLint("NewApi") to the declaration of your functioin, this is:
#SuppressLint("NewApi")
public void init_download(String title, String filename, String url) {
}
You can solve the warning with #SuppressLint("NewApi")but in lower API's your setNotificationVisibility will not work as there is no DownloadManager in support packages.
I've solved the last problem by changing the VISIBILITY of the notification:
public void init_download(String title, String filename, String url) {
File path = new File(Environment.DIRECTORY_DOWNLOADS);
if (!path.exists()) {
path.mkdirs();
}
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(title)
.setDescription(getResources().getString(R.string.downloading))
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
else {
request.setShowRunningNotification(true);
}
long id = downloadManager.enqueue(request);
//Save the request id
SharedPreferences.Editor PrefEdit = shared_pref.edit();
PrefEdit.putLong("DOWNLOAD_ID", id);
PrefEdit.commit();
}
This is to download a file to the Downloads directory of your android device.
You should add #SuppressLint("NewApi") before your method. But it will be better to up min sdk version or don't use this method. If you add #SuppressLint("NewApi") don't forget about users that can run you app on device with 9 api.

file is not downloading via DownloadManager in Android

Hi i want to download video file and below is my code
public void file_download(String uRl) {
File direct = new File(Constant.FOLDER_PATH);
try {
uRl = "http://songs7.funmaza.in/videos/"
+ URLEncoder
.encode("Issey Kehte Hain Hip Hop 720p - Yo Yo Honey Singh [Funmaza.com].wmv",
"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("URL For Download == " + uRl);
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mdDownloadManager = (DownloadManager) ((Activity) context)
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(uRl));
request.setDescription("Downloading via Your app name..");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(direct));
mdDownloadManager.enqueue(request);
}
and in download manager I got following information
If i pass simple URL like "http://beta-vidizmo.com/hilton.mp4" then it is working fine
Try this way,hope this will help you to solve your problem.
file_download("http://songs7.funmaza.in/videos/Issey Kehte Hain Hip Hop 720p - Yo Yo Honey Singh [Funmaza.com].wmv",context);
public void file_download(String url,Context context) {
url = url.replace(" ","%20");
DownloadManager downloadManager = (DownloadManager) ((Activity) context).getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Demo")
.setDescription("Downloading via Your app name..")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory() + "/MyFolder", "test1.3gp");
downloadManager.enqueue(request);
}
Finally Try and error, I found this solution,
File direct = new File(Constant.FOLDER_PATH);
uRl = "http://songs7.funmaza.in/videos/Issey Kehte Hain Hip Hop 720p - Yo Yo Honey Singh [Funmaza.com].wmv";
uRl = uRl.replace(" ", "%20");
uRl = uRl.replace("[", "%5B");
uRl = uRl.replace("]", "%5D");
System.out.println("URL For Download == " + uRl);
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager downloadManager = (DownloadManager) ((Activity) context)
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(uRl));
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setDescription("Downloading via Your app name..")
.setTitle("Issey Kehte Hain Hip Hop")
.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir("/MoviesAndSongs",
"test1.mp4");
downloadManager.enqueue(request);
It's done :) Thanks Haresh to you also

Categories

Resources