philtz
philtz

Reputation: 226

android path to my file

I'm using a very complex method (not written by me) in my android app, which takes a String parameter "path" and then opens and parses the file from that path. The problem is this: When i set the path to a file on my sdcard (like this: Environment.getExternalStorageDirectory()+"/myfile.txt" ) it works fine. But i don't want my file to be available for the user so i tried to set the path to the assets folder in my project where i copied the file, and using this path it won't work.The path I used for the file in my assets folder wast this: file:///android_asset/myfile.txt So why the first path works fine and the second does nothing?

Thanks

Upvotes: 0

Views: 1680

Answers (2)

philtz
philtz

Reputation: 226

So what i did was this: i wrote a method that copies the file from the assets to the data directory of my app and then sending this path to the "very complex method". Now it works fine. Code sample (maybe it will help others in the future):

///opening the file from the assets folder

InputStream in = getAssets().open("myfile.txt");

///creating the output and the location to it

///you can set any path you like

(new File("data/data/myAppPackageName/databases/")).mkdirs();

OutputStream out = new FileOutputStream("data/data/myAppPackageName/databases/myfile.txt");

///copy method from "in" to "out"

Path sent as parameter to the "very complex method": "data/data/myAppPackageName/databases/myfile.txt"

Upvotes: 0

Timuçin
Timuçin

Reputation: 4673

You have to use AssetManager to access the files in assets folder.

Upvotes: 1

Related Questions