Environment.getExternalStorageDirectory() not working in Android - 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

Related

How to get file path inside folder structure

I am looking for a maybe better solution to get the path of a file inside a folder.
For example:
I have the absolute path: "/storage/emulated/0/Mobile Pauker++/Lessons/Berge/Hoechste-Berge.pau.gz"
And I want to get: "Lessons/Berge/Hoechste-Berge.pau.gz"
My current solution is:
filepath.substring(filepath.indexOf("Mobile Pauker++")+15)
But I don't think that this is a good solution.
Please try this val path = File(Environment.getExternalStorageDirectory().toString() + File.separator + "Lessons/Berge/Hoechste-Berge.pau.gz")
I think I found a good solution:
filepath.split("${Environment.getExternalStorageDirectory()}${Constants.DEFAULT_APP_FILE_DIRECTORY}")[1]
I give the .split() method the path of my "root folder" and take the second part.

How to create folder in root of storage Android

I'm new at this.
Telegram, Whatsapp, Vk, Aliexpress - All this apps can write they folders in /Storage/emulated/0
How can I create my folder in this place for Android 5...10?
When I try to use File("/storage/emulated/0/", myfile) - it doesn't work.
Please, can anyone give some mini example how can I create my files in storage
Since you haven't written any code, I can't tell you what you're doing wrong. But this is something that might help you.
First you need to declare permissions on your manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
After that you can simply use this to create your folder.
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
"folderName";
File directory = new File(path);
directory.mkdirs(); // It returns a boolean you can use it to check if your folder
// was created.
Yes, You can create Folders inside the External Storage too.
But, keep in mind that you cannot use emulated/0 because it is different for different devices.
File path = new
File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Folder/");
if(!path.exists()){
boolean k = path.mkdirs();
}
Although, Environment.getExternalStorageDirectory() is deprecated as of API29. So You can use getExternalDir() too.

How to build File Uri without newing a file?

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.

android: Check the existence of a file in the application

I have few html files in assets folder of my application. My application loads these files depending on the device language. When I check for the existance of the file it say does not exist, but when I load that file using browser.loadUrl(filename), it loads it fine.
Following code will help you to understand my problem:
String filename="file:///android_asset/actualfilemname.html";
File f = new File(filename);
if(!f.exist){
filename = "file:///android_asset/newfile.html";[Everytime it loads this file even though I have actualfilename.html in the folder]
}
browser.loadUrl(filename);
[it loads the newfile.html but not actualfilename.html]
You can't use File for resources. You'll need to use the AssetManager for that.
(In the off-chance that File does handle resources, which I don't think it does, you'll have to convert the path to a URI first, for example using URI.create(). File(String) expects a path, not a URI.)
Is this the exact code you are using? you probably want to be calling f.exists() not filename.exist().
Edit: try working with the AssetManager instead of hard coding your file path. My best guess is that the file path you are using is not exactly how it supposed to be.

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