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);
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.
Since android 6.0, the sdcard path is no longer "/storage/sdcard1/", "/storage/sdcard-ext/" or something.
The path depends on the phone instead. If I use Nexus 5x AVD, the path is "/storage/1D15-3A1B/". When I use Nexus 6p AVD, the path is "/storage/4679-1802/". So how can I dynamically write the sdcard path in program to locate the file in external sdcard?
Thank you!
Have a look at getExternalFilesDirs().
It seems that I found the solution.
I'm using the access storage framework. I get it by handling the string of uri and get the "1D15-3A1B" part and then combine it to the sdcard path. Here is the solution.
When I click the file Welcome to Word.docx in the sdcard root path, I get the intent by onActivityResult().
Get the DocumentId by String docId = DocumentsContract.getDocumentId(intent.getData()), thus the string docId is "1D15-3A1B:Welcome to Word.docx".
Split the string and get a string array by String[] split = docId.split(":"), so we can get "1D15-3A1B" which is split[0], and the path in sdcard "Welcome to Word.docx" is split[1].
If we want to get the uri of the file we clicked before, just need to build a new string String newPath = "/storage/" + split[0] + "/" + split[1].
update:
Actually getExternalFilesDirs() is much easier to do this...Thank you #greenapps!
I was trying many approaches, including:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
Environment.getExternalStorageDirectory()
Still my picture files in DCIM/Camera were not visible to my app. I could see the directory names, but listFiles() on any of these directories returned null.
Finally, and sheepishly, I found that my Manifest did not contain the magic line android.permission.WRITE_EXTERNAL_STORAGE.
Once I added the permission, it worked like a charm.
Moral of the story: Android does not warn/break/throw exception under this condition. When it fails, it fails silently!
Hope the subject line makes it clear.
I want to get hold of the file path of the xml file which i store under the res/xml folder.
I'm aware of the InputStream approach by directly reading using the snippet.
... getResources().getXml(R.raw.testxml);
but my requirement is to just get the path of the stored file for the moment.
Any help appreciated
VATSAG
try as to get full path of file stored in res/raw folder
String fullpath = "android.resource://" +
YOUR_FULL_PACKAGE_NAME +
"/"+R.raw.testxml;
So in my Eclipse android project I have a pdf file that I'd like to open, I looked up the standard address on the android developer's page and I came up with this pointer:
File file = new File("Android/data/com.alex.testing.app/res/raw/jazz.pdf");
where jazz.pdf is situated in res->raw in my eclipse project,
and com.alex.testing is my package name.
Still, when I try if(file.exists()) , the function returns false (on the emulator it goes to an else I've set up to display an error message)...
Sorry for the newbie question, but I'm really stuck with this :(.
put the file in assets folder and pick the file from there
Now use Context.getAssets().open("jazz.pdf") and pass the resulting InputStream into PDf parser library
Ok, to access resources from current application you can use something like,
Uri path = Uri.parse("android.resource://<you package>/raw/<your_file.pdf>");
OR
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" R.raw.<your_file.pdf>);
But I have a doubt if you are trying to use this pdf file in your application then its OK, but If you want to view this file using any third party application then I think you can't do it.
Because external application can't access application's package resources file.
So better way it to put this file in /asset directory then copy it to any public access area then view that file from that path.
//if your are stored in SDcard your location would be
"data/data/com.alex.testing/res/raw/jazz.pdf"
//you read resources from raw folder thru the below code
InputStream inputStream = getResources().openRawResource(R.raw.jazz);
byte[] reader = new byte[inputStream.available()];
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);