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.
Related
I have successfully downloaded a .Zip file from my server, which is then stored on my tablet.
However, I now want to set VideoView.setUri() to one of the files in the .zip.
I have DownloadManager.getUriForDownloadedFile(referenceId) showing me that the zip's Uri is: content://downloads/my_downloads/305.
What I want to do now is take that Uri and feed it into the VideoView, without extracting it (if possible).
I have found that you can reference a zip's file via the extension:
"zip_path_zip_name" + "!/" + filename.ext".
So you could call:
VideoView.setVideoURI(Uri.parse("zipPathAndTitle" + "!/" + "test.mp4""));
But I'm stuck from the Uri of: content://downloads/my_downloads/305 getting that as a file I can reference, to then set as the VideoView's Uri.
Any help is greatly received.
First, extract the zip file to a specific folder in your storage. You can't use file inside this zip file unless you extract it. Get the path of the extracted file you want in this folder then set it to your VideoView.
I want to get fileName from a uri... I can get a fullpath from Other apps like gallery or other third party file managers, but I can't get fileName from KitKat Document, I don't need a fullPath just the fileName with an inputStream is good for me...
Thanks
Edit: I found a piece of code that solves the issue almost, but it can't get filePath of none known types like *.exe *.vcf , ...
Here's the source
If you have the absolute path of file then simpley create the object of file and use getName method
File f = new File(<Full Path of your file>);
This will return file name with extension.
ANd if you do not want extension
then use CommonsI/O libarary
And from that library you can use
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);
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...
In Android webview, you could pass something like this file:///android_asset/myfile.txt or this http://www.mysite.com/t.html and it will get it without a problem. Now if I tried the first url with the file prefix in a reader "FileReader" it will throw an exception that File not found although in WebView it work with no problem (why?)
What I want is to create a function that could take a file or http url and get the content html using the full path "file:/// ..." how could I do this without facing the file not found exception ?
Now if i tried the first url with the file prefix in a reader "FileReader" it will throw an exception that file not found although in WebView it work with no problem (why?)
Because "File Reader" does not have an asset named /myfile.txt, presumably. "File Reader" is somebody else's program, not yours, so file:///android_asset/myfile.txt refers to an asset in that other program, not yours.
What i want is to create a function that could take a file or http url and get the content html using the full path "file:/// ..." how could i do this without facing the file not found exception ?
Have an asset named /myfile.txt in your app.
I have not tried using standard Java I/O (e.g., File objects) to read file:///android_asset paths. That might work. If not, use startsWith() to determine if the string begins with file:///android_asset -- if it does, trim that off and use the rest with AssetManager to read the asset.
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);