im not getting default file name when i download - android

i have created a webview app i added downloader inside which download the file end with m4a..the app downloads the file but file name changes...how can get title from the file...
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// handle different requests for different type of files
// this example handles downloads requests for .m4a and .mp3 files
// everything else the webview can handle normally
if (url.endsWith(".m4a")) {
Uri source = Uri.parse(url);
// Make a new request pointing to the .apk url
DownloadManager.Request request = new DownloadManager.Request(source);
// appears the same in Notification bar while downloading
request.setDescription("Description for the DownloadManager Bar");
request.setTitle(getTitle());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
// save the file in the "Downloads" folder of SDCARD
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "filename");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
else if(url.endsWith(".mp3")) {
// if the link points to an .mp3 resource do something else
}
// if there is a link to anything else than .m4a or .mp3 load the URL in the webview
else view.loadUrl(url);
return true;
}
});

This will give you your file name
final String[] separated = url.split("/");
final String myFile = separated[separated.length - 1];
It will split the url using the / character and you take the last element in the returned array.
Arrays are 0 based, so the last element is the one located at the vector's length - 1.
Put the above code just before this line: if (url.endsWith(".m4a")) {, where you want to get your file name.
Then, use it so:
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, myFile);

Full code for downloading file inside webview without calling web-browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// handle different requests for different type of files
// this example handles downloads requests for .m4a and .mp3 files
// everything else the webview can handle normally
if (url.endsWith(".m4a"))
{
Uri source = Uri.parse(url);
final String[] separated = url.split("/");
final String myFile = separated[separated.length - 1];
// Make a new request pointing to the .apk url
DownloadManager.Request request = new DownloadManager.Request(source);
// appears the same in Notification bar while downloading
request.setDescription("Description for the DownloadManager Bar");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
// save the file in the "Downloads" folder of SDCARD
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, myFile);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
else if (url.endsWith(".pdf"))
{
Uri source = Uri.parse(url);
final String[] separated = url.split("/");
final String myFile = separated[separated.length - 1];
// Make a new request pointing to the .apk url
DownloadManager.Request request = new DownloadManager.Request(source);
// appears the same in Notification bar while downloading
request.setDescription("Description for the DownloadManager Bar");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
// save the file in the "Downloads" folder of SDCARD
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, myFile);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
// if there is a link to anything else than .m4a or .mp3 load the URL in the webview
else view.loadUrl(url);
return true;
}
});

Related

How to download image from Webview to custom/private folder instead of Download folder in Android

I am trying to download image from webview in custom folder of Android app. As of now i can only download file in Download folder by using below code.
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(imgUrl));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
Is there any way to store image in other folder (newly created folder) instead of default Download folder. Actually i want to download image in Android app folder that will be private and not accessible to user via File Manager or other Apps.
I tried to create folder by using following code but folder is creating under /Android/data/com.abc.xyz/ is there any way to create folder directly under internal storage (under root directory) ?
File direct = new File(Environment.getExternalStorageDirectory()+"folder_name");
if(!direct.exists()) {
if(direct.mkdir());
}
try this
mywebView.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);
File folder = new File("/storage/emulated/0/Folder_Name");
if (!folder.exists()) {
folder.mkdirs();
System.out.println("Directory created");
} else {
System.out.println("Directory is not created");
}
request.setDestinationInExternalPublicDir("Folder_Name","Wallpaper.jpg");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});

Image URL is downloaded as .Zip format

I have used DownloadManager class for image download. When I have used below image url in browser it is working fine. but when i have downloaded that image url using DownloadManager it is getting .zip format.
Image Url : Here
Below is my code of download Manager :
private void startDownload(String url) {
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(true);
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
request.setMimeType(mimeString);
request.setTitle(getString(R.string.app_name));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription("Downloading " + txtDocName.getText().toString());
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, System.currentTimeMillis() + ".jpeg");
downloadManager.enqueue(request);
AppLog.showD(TAG, "downloadind started");
}
The provided URL returns zip as a type. You can check that using any dev tool on your browser as demonstrated in this screen shot

How to detect the extension of file to download using DownloadManager in Android?

I start developing my very first Android project. In my project, I need to download media files, especially mp3 or mp4. I am downloading file using DownloadManager.
Here is my download code for mp3
private void downloadPodcast(int id)
{
String url = context.getResources().getString(R.string.api_endpoint)+"podcast/download?id="+String.valueOf(id);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading...");
request.setTitle("Podcast");
request.setMimeType("audio/MP3");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "audio.mp3");
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
As you can see in my code, I am downloading only mp3 and setting the MIME type is constant. The file name and its extension is constant as well. What I want to do is to detect the file extension I will download. So can I set the MIME type programmatically and set the extension of file name. How can I achieve it in DownloadManager?
Though its quite late, however here is the answer:
You can get the extension of the file to download using the code below and add the extension to your file name.
String fileUrl = "http://someurl";
String fileName = "foobar";
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
// concatinate above fileExtension to fileName
fileName += "." + fileExtension;
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl))
.setTitle(context.getString(R.string.app_name))
.setDescription("Downloading " + fileName)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);

downloading files using dropbox url android

I am trying to download files using dropbox url. I copied a code from Download a file with Android, and showing the progress in a ProgressDialog which uses DownloadManager class.
public void downloadFromDropBoxUrl(View view) {
//verfying if the downloadmanager is available first.
if (isDownloadManagerAvailable(getApplication())) {
String url = "https://www.dropbox.com/s/m4z5u9qstxdtbc3/AllExams22.pdf?dl=0";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Some descrition");
request.setTitle("Some title");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my-map.pdf");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
}
public static boolean isDownloadManagerAvailable(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
return true;
}
return false;
}
It is working on achiving direct files with urls but this time I am trying to do it with dropbox share links but its not working out. I don't want to connect to dropbox api. I think it is useless. Is there any way I can download files directly from the dropbox url?
just replace
String url = "https://www.dropbox.com/s/m4z5u9qstxdtbc3/AllExams22.pdf?dl=0";
by:
String url = "https://dl.dropboxusercontent.com/s/m4z5u9qstxdtbc3/AllExams22.pdf";
Then:
final DownloadTask downloadTask = new DownloadTask(YourActivity.this);
downloadTask.execute(url);
Please see this link on how to download a shared file from Dropbox
https://blogs.dropbox.com/developers/2013/08/programmatically-download-content-from-share-links/

How I can download video in my application like we download or install apk file on google play?

I want to download video from URL in background like we install apk file in background from Google play. I want to show downloading indicator on notification bar same like it showing while we download or install apk file. if any suggestion or idea then suggest me how I can achieve this task in my project.
You can prefer android DownLoadManager. somehow want to do like this in your activity
String mUrl="your video url";
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(Uri.parse(mUrl));
request.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, your_fileName);
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setAllowedOverRoaming(false);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
manager.enqueue(request);
setNotificationVisibility() -> show download notification in notification bar(window) with download progress also.
below are three methods where you can choose destination for your downloaded file path.
1. setDestinationInExternalFilesDir(Context context, String dirType, String subPath)
Set the local destination for the downloaded file to a path within the application's external files directory (as returned by getExternalFilesDir(String).
2. setDestinationInExternalPublicDir(String dirType, String subPath)
Set the local destination for the downloaded file to a path within the public external storage directory (as returned by getExternalStoragePublicDirectory(String)).
3. setDestinationUri(Uri uri)
Set the local destination for the downloaded file.
Use Download Manager,
private void for_call_download() {
File folder = Environment.getExternalStoragePublicDirectory(DOWNLOAD_FOLDER_NAME);
if (!folder.exists() || !folder.isDirectory()) {
folder.mkdirs();
}
try {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(APK_URL));
request.setDestinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME, DOWNLOAD_FILE_NAME);
request.setTitle(SessionName);
request.setDescription("" + SessionDesc);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(false);
request.setMimeType("application/cn.trinea.download.file");
downloadId = downloadManager.enqueue(request);
} catch (IllegalStateException i) {
showAlert(context, "Sorry Some Problem");
i.printStackTrace();
}
updateView();
status_download = true;
}

Categories

Resources