开发者

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 folde开发者_开发百科r wast this: file:///android_asset/myfile.txt So why the first path works fine and the second does nothing?

Thanks


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


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜