i have used the below code to download a video from server in android studio and it works correctly , but when i search the video in my device i cant find it anywhere ... where does it save and how can i change destination directory on "internal storage"?
private DownloadManager downloadManager;
btn_download_video.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(urlVideo);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(Video_detail_Activity.this, Environment.DIRECTORY_DOWNLOADS, videoName);
Long reference = downloadManager.enqueue(request);
}
});
i used below permissions in my project too:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
thanks for helping ... as pskink said i changed
request.setDestinationInExternalFilesDir(Video_detail_Activity.this, Environment.DIRECTORY_DOWNLOADS, videoName);
to
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS.toString(), videoName+".mp4");
and now i can see downloaded file in "Download" folder.
Related
So i have used DownloadManager to download a file from the internet using the following code :
public void DownloadFile()
{
Log.d("DownloadFileEntered", "true");
try {
String url = "https://firebasestorage.googleapis.com/v0/b/roti-bank-testing.appspot.com/o/SocialGallery%2Fsome.jpg?alt=media&token=d2ddd40f-0c04-441c-82b1-585fd8743b19";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "somegif.gif");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long d = manager.enqueue(request);
}catch (Exception e)
{
Log.d("DownloadFileEntered", "Error."+e.toString());
}
}
Now say, i want to delete this file.
So to do that, i used the following code :
File file = new File("file://" + Environment.DIRECTORY_DOWNLOADS + "/somegif.gif");
boolean d = file.delete();
Log.d("DownloadFileEntered", "D : "+d);
But apparently, the last Log.d gives the output as false, and the file is also not deleted. So, what is the correct way to delete a file from the external storage?
Also, can we run a function when the download is completed ? if yes then how ?
Hope this will help you:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/somegif.gif");
boolean d = file.delete();
Log.d("DownloadFileEntered", "D : "+d);
And don't forget to set this permission in your manifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
I'm trying to develop an app which provides many movies and you can play it or download the movie.
I'm using Download Manager to download the movie from server, but it didn't work...
this is my method
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 | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(true);
DownloadManager downloadManager = (DownloadManager)getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
long id= downloadManager.enqueue(request);
}
catch (Exception ex){
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
this is attribute
url: (http://192.168.1.5:8080/BlueNet_NMC/video_shared/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" />
but when I test it in emulator or in my Galaxy S4 not working.
Please help me.
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 do i make it open the default download manager app by clicking on a file being downloaded from the statusbar? i want to handle the downloads in the same webvew, not in any other browser. this method handles it but i am not able to click the downloading files-
// Download manager
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
also it downloads files without it's original name . every file is called "download" i want to fix this
I'm using DownloadManager to download a file from a webService. The download end successfuly, but when I try to open the new file in the "download" folder I've got this error "Impossible to open file" (and I know I can open this type of file).
Also, when I plug my phone to my computer and when I open the download file with it, the file open successfuly and is not corrupted.
I don't have other error, so I'm really lost !
Here my code:
/*Data*/
int filePosition = position - _subFolderNameList.length;
String url = _folder.getFiles().get(filePosition).getUrl();
String FileName = _folder.getFiles().get(filePosition).getName();
String Description = _folder.getFiles().get(filePosition).getUrl();
/*Prepare request*/
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(Description);
request.setTitle(FileName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, FileName);
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request); // Send request
Edit: Permission in the Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Okay I found the problem !
I had to specify the "MIME content type" of the file using setMimeType().
public DownloadManager.Request setMimeType (String mimeType);
Yes you have to specify the MIME type.
Adding a few more details to the accepted answer.
public DownloadManager.Request setMimeType (String mimeType);
To get MIME type you can use the following function.
private String getMimeFromFileName(String fileName) {
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(fileName);
return map.getMimeTypeFromExtension(ext);
}
and the following is the Xamarin.Android implementation of the same :
private static string GetMimeTypeFromFileName(string fileName)
{
var map = MimeTypeMap.Singleton;
var ext = MimeTypeMap.GetFileExtensionFromUrl(fileName);
return map.GetMimeTypeFromExtension(ext);
}