How does the Android Downloadmanager work? - android

I'm trying to download a file to the files-folder of the app
/data/user/0/app-name/files/
If I enqueue the download, the app crashes and I don't know why.
Trying setDestinationUri() AND setDestinationInExternalPublicDir() both won't work.
What is the big mistake?
DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("https://testpage.com/download.php");
DownloadManager.Request request = new DownloadManager.Request(uri);
//File fileDl = new File(getApplicationContext().getFilesDir() + "/");
String dFolder = getFilesDir().toString();
request.setDestinationUri(Uri.parse("file://" + dFolder + "/"));
//request.setDestinationInExternalPublicDir(fileDl + "/", "epaper.pdf");
downloadmanager.enqueue(request);
I know some of you can see the misunderstanding, but as a Newbie I'm blind :-)
Can anybody help?
Thx,
Chris

Related

my pdf document is not getting saved on my phone

i am trying to save a PDFDocument on my phone but i am not able too. this is my code;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),"/" +nameGet1 + "-" + idGet1+".pdf");
Try using this code, and make use you have permission to save on phone
download.setOnClickListener(v -> {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(your_file_url));
request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(your_file_url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, your_file_name);
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
});

android 10 : android.process.media keeps stopping when invoke downloadmanager

Recently I start testing my app in Android 10 (it works fine on the previous version). When I invoke download manager for a file, I got a crash with a message:
android.process.media keeps stopping
This is my snippet code:
String mFilePath = "file://"
+ FileManager.getInstance(getApplicationContext()).getDir()
+ "/"
+ fileName
+ extension;
Uri downloadUri = Uri.parse(fileUrl);
DownloadManager.Request req = new DownloadManager.Request(downloadUri);
req.setDestinationUri(Uri.parse(mFilePath));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(req);
What's the exact problem? How can I report this error to Gooogle?

How to prevent application file deleted when uninstall the app in android

DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(file_url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setTitle("")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES + "/MyApp", tsLong.toString() + ".jpg");
The path to download file is here:
new File(Environment.DIRECTORY_PICTURES + "/MyApp")
I found that once the app is uninstall the folder is also deleted, how to preserve that ?
thanks a lot.

Download Image and store it in Gallery app in android

I want to save Image file in Gallery so that Image can be viewed from the Gallery application.
But what I want is to create a separate dir, as like we have for whatsapp Images etc apps in our gallery application.
So far I have written this code to download an image
public void createDir(){
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), DIR_NAME);
Log.d(LOG_TAG, "dir pictr :" + dir.toString());
if (!dir.exists()) {
dir.mkdir();
Log.d(LOG_TAG, "dir not exists and created first time");
} else {
Log.d(LOG_TAG, "dir exists");
}
}
Above code created directory inside gallery dir
Uri imageLink = Uri.parse(downloadUrlOfImage); // this is download link like www.com/abc.jpg
CreateDir();
DownloadManager.Request request = new DownloadManager.Request(imageLink);
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), DIR_NAME);
String absPath = dir.getAbsoultePath();
request.setDestinationUri(Uri.parse(absPath + "image.jpg"));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager dm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
But this gives me error as java.lang.IllegalArgumentException: Not a file URI: /storage/sdcard0/Pictures/FreeWee/1458148582.jpg
Basically what I want is to save Image and that image must be shown in Gallery Application under Some directory I named.
If not understood please ask, so that I can improve my question.
How do I proceed further ?
As #RoyFalk pointed out you got 2 issues in your code.
So you can go with this code snippet
String filename = "filename.jpg";
String downloadUrlOfImage = "YOUR_LINK_THAT_POINTS_IMG_ON_WEBSITE";
File direct =
new File(Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
.getAbsolutePath() + "/" + DIR_NAME + "/");
if (!direct.exists()) {
direct.mkdir();
Log.d(LOG_TAG, "dir created for first time");
}
DownloadManager dm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(downloadUrlOfImage);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(filename)
.setMimeType("image/jpeg")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,
File.separator + DIR_NAME + File.separator + filename);
dm.enqueue(request);
And you will see Image inside the gallery application under your DIR_NAME.
Hope this will help you.

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);

Categories

Resources