My HTTP-server allows downloading files with a 'dynamic' url: e.g. http://myserver.com/query?id=12345 which will give me my_song.mp3.
The filename is indicated in the content-disposition header.
Downloading this kind of file with Android DownloadManager works fine but I want to be able to control where the file is being saved to.
The normal way to do this would be to call
DownloadManager.Request r = new DownloadManager.Request(uri);
r.setDestinationInExternalPublicDir(String dirType, String subPath);
Unfortunately this requires to know the filename up front which I don't know. I tried calling the above function with a null for subPath but it does not work...
Related
I´ve an app, in which user can download files. For the download-process i use androids DownloadManager class. The file-download works perfectly, but i want to achieve, to store the file using the orginal file name of the downloaded file from the internet url. I´ve searched in the web and found different methods. The most just try to parse to file name from the url like so:
URL: https:// mysite.com/ filename.txt
parse the file name with url.substring(url.lastIndexOf('/') + 1) an you will have your file name...
But i need a method, that gets the file name also, if the file name is not included clearly in the url.
I also found some method to get the file name with
URLUtil.guessFileName(url, contentDisposition, mimetype)
I´ve implemented this method, but i´am getting always "download.bin" as my file name , which is incorrect. Maybe i have incorrect contentDisposition or mimetype for this method. How do you create contentDisposition or mimetype from just an url?
So i´m a bit helpless with this. Is there a safe working method with this.
So my program works like this:
It downloads a .txt file from the dropbox and saves it to downloads directory.
Uri uri = Uri.parse(file);
DownloadManager.Request r = new DownloadManager.Request(uri);
// This put the download in the same Download dir the browser uses
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "upload");
r.allowScanningByMediaScanner();
// Start download
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(r);
And the code is working fine, it downloads the file called upload.txt.
So now i want to read from it. I have been looking for some codes and nothing seems to be working...
Here is my current code, it throws a FileNotFoundException but file is located there.
Any tips?
String text="";
FileInputStream Fin=new FileInputStream(Environment.DIRECTORY_DOWNLOADS+"upload");
byte[] b=new byte[100];
Fin.read(b);
text=new String(b);
Fin.close();
Ofc i have tried putting "upload.txt", and even "/upload" and "/upload.txt" but still nothing.
If anyone can give me code that reads a text file, it would be awesome, or if someone could give me a code that can get the source code form a html site ( without JSOUP and other parsers, i have tried that, i want to implement my own parser ).
Thanks!!
Change
FileInputStream Fin=new FileInputStream(Environment.DIRECTORY_DOWNLOADS+"upload");
to
FileInputStream Fin=new FileInputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "upload"));
You should use Environment.getExternalStoragePublicDirectory to get the path.
Or if you like more,
FileInputStream Fin=new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/upload");
Change
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "upload");
too with
r.setDestinationInExternalPublicDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "upload");
Took me ages, but finally:
You should add to the manifest the various permissions (android.permission.READ_EXTERNAL_STORAGE...)
But this is not enough!
you should explicitly ask for these permissions in the onCreate():
requestPermissions({android.permission.READ_EXTERNAL_STORAGE,...}, 200);
Only afterwards you can access these directories.
I'm trying to download a file to directory that can vary based on whichever directory the user chooses. I store the current directory in a file object and now attempting to download the file. The file downloads, but it doesn't download the specified directory. So, what can I do to get the file in the directory selected.
// Getting path to store the file
String path = root.getAbsolutePath();
path += curr.getName();
request.setDestinationInExternalPublicDir(path, new File(url).getName());
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
The String named url, that is being passed to the File constructor, contains a URL. Just using that to retrieve that name of the file.
UPDATE:
I just found the file. Its in the phone not the sd card. It was in this folder, storage\emulated\00. Not sure why? Also, the absolute path that I'm getting is storage\emulated\0.
So, I figured out that the path returned is always storage\emulated\0 for the call getExternalStorageDirectory().getPath()) to the Environment. Which is what I using to the get the initial directory. So for instance if I navigated through the folders to the Downloads folder the path would be storage\emulated\0\Download. When I was downloading the file though instead of going to the actual downloads folder it would go to, or create, the downloads folder in the emulated folder in the storage folder. To resolve this I found the index of the zero in the path, added 1, and got the substring using that. After that it worked. Also I had to do this in another method where I navigate through the directory. As a parameter I pass in the file and then within the method I get the substring. Within the method that I use to download the file I get storage\emulated\0 no matter what. Not sure why it does this. If anyone could explain this it would be greatly appreciated.
Does root.getAbsolutePath() return a string with a path separator character at the end?
If not, then you might be doing something like bin/usr/var/appfilename.ext instead of bin/usr/var/app/filename.ext, because you're concatenating it straight into name of the file.
I still had this issue on Android 7. In my case, the file would not show up until I restart the device. This a known issue: https://issuetracker.google.com/issues/36956498
Source: https://stackoverflow.com/a/20413888/2552811
I'm having problems loading an external html file into the webview. I've done this before and it should be easy, but for some reason I keep getting Web page not available.
I know the files are in the directory because I placed them myself using file explorer.
String filename = "file:///"+ Environment.getExternalStorageDirectory() + File.separator + "Android/data/com.example/files/test_html2.html";
webview.loadUrl(filename);
I've tried moving the files into root and trying there, I've removed file:// and replaced it with content:// and nothing at all. I have read permissions in the manifest.
Any ideas?
Don't create file:// URLs yourself, as you will tend to screw them up. In this case, I think that you have four slashes after the :, three that you typed in and one from Environment.getExternalStorageDirectory().
Instead, create a File object and use that as the basis:
File f = new File(Environment.getExternalStorageDirectory(), "Android/data/com.example/files/test_html2.html");
webview.loadUrl(f.toURI().toURL()); // or use Uri.fromFile(f).toString() instead
I'm trying to post a notification that lets the user open a locally stored file. My code looks like this:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(filename));
notificationIntent.setData(uri);
Where "filename" is the full path to a locally stored file, usually in the /mnt/sdcard/download directory. The files I want to display are of various types: images, PDF documents, HTML, etc.
This works, but sometimes Android tries to open the file as the wrong type. For example, a jpeg file will open in a web browser view and instead of seeing the image, I see the binary data from the file displayed as text. Other times it works file. For example, some PDF files correctly open in a PDF viewer and some do not.
I'm not sure why this is. The documentation says I should not have to pass an explicit content type. If I do set the content type explicitly, things seem to work fine. The problem is, I don't always know what the content type should be (the file is downloaded from an external source and can be anything, and no, the MIME type is not in the HTTP headers, I checked for that).
What can I do here? Is there some function I can call with a filename to have Android return me the best content type for that file? Moreover, why is this not happening automatically when the Intent is processed?
Thanks.
You've most likely figured this out; I'm posting in case someone else is stuck on this. I do the following to get the mime-type of the file:
//Get the file path
Uri path = Uri.fromFile(file);
MimeTypeMap type_map = MimeTypeMap.getSingleton();
//Get the extension from the path
String extension = MimeTypeMap.getFileExtensionFromUrl(path.toString());
extension = extension.toLowerCase();
if (extension.contains(".")) {
extension = extension.substring(extension.lastIndexOf("."));
}
String mime_type = type_map.getMimeTypeFromExtension(extension);