How to build File Uri without newing a file? - android

I need to build a Uri object from a file path, but using Uri.fromFile(new File(path)) is too slow, so I want to build it manually.
First of all, Uri.parse("file://" + path) does not work, since it does not path-encode the path.
I tried Uri.Builder().scheme("file").path(orgPath).build(), but the result is: file:path instead of file://path.
How can I build a Uri as same as Uri.fromFile() does, in a faster way?
Thank you!

try Uri.encode()
"file://"+Uri.encode(path)
or if you want to allow the string like / or any other than pass it as second parameter
like :
"file://" + Uri.encode(path,"/")

OK I find out I just need to add .auth().
Uri.Builder().scheme("file").auth("").path(orgPath).build() is working fine.

Related

Can not locate the sdcard file(not hard code) in android 6.0

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!

What's the difference between File.toURI vs Uri.fromFile in Android

While trying to get a picture using a Camera intent as describe in the documentation.
I've added the required MediaStore.EXTRA_OUTPUT extra but the problem was that it didn't save the file where I wanted. So I read the documentation more carefully, and compared it with what I was doing (since I didn't blindly copy/pasted the provided sample code), and found that I was using
intent.putExtra(MediaStore.EXTRA_OUTPUT, myFile.toURI());
Instead of
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myFile));
The second version worked perfectly, but I can't figure out why. The documentation for both methods don't state anything related to incompatibility between the two.
Shouldn't the two give the same result, or am I missing a subtle difference?
Just from a quick test here:
Uri.fromFile() returns a Uri (android.net.Uri)
file.toURI() returns a URI (java.net.URI)
I expect this is causing the problem.
The content produced by Uri.fromFile() and file.toURI() is not the same...
for the same file "f" located on the sdcard.
"f.toURI()" will add this into your Bundle {output=file:/mnt/sdcard/Gp/Gp.db}
and "Uri.fromFile(f)" will add {output=file:///mnt/sdcard/Gp/Gp.db}

Environment.getExternalStorageDirectory() not working in Android

As i need to load a file from SDCard when i am using Environment.getExternalStorageDirectory() or Environment.getExternalStorageDirectory().getAbsolutePath() but it is not working, well it works great with the "file:///sdcard/Avalon/assets/www/filename.html". I don't know what is happening with my path
cwv.loadUrl(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Avalon/assets/www/filename.html", map);
I want make my path generic that's why i need to work it with any generic term...
Great it works... Actually i forgot to add "file://"
So actual path should be
cwv.loadUrl("file://"+Environment.getExternalStorageDirectory().getAbsolutePath()+"/Avalon/assets/www/filename.html", map);
This is my mistake thanks

Get fileName from uri on KitKat Document

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

Android : Adding image in email

I want to add image in email, if I add the file from sdcard then its working fine as given below.
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file///sdcard/Images/thumb.jpg)"));
But I want to add the image from assest. I have tried so many pernutation and combination with uri.parse and uri.fromfile but nothing works, can anyone tel how exactly the path should be to add the file from assets. Its very urgent.
isnt it?:
file:/// instead of file///
... you missed the ":"
Refer http://developer.android.com/reference/android/content/res/AssetManager.html

Categories

Resources