我的文件的 android 路径
我在我的android应用程序中使用了一个非常复杂的方法(不是我写的),它接受一个字符串参数“path”,然后打开并解析该路径中的文件。 问题是这样的:当我设置 SD 卡上文件的路径时(如下所示: Environment.getExternalStorageDirectory()+"/myfile.txt" ),它工作正常。 但我不希望我的文件可供用户使用,因此我尝试在复制文件的项目中设置资产文件夹的路径,并且使用此路径将不起作用。我用于我的资产文件夹中的文件是这样的:file:///android_asset/myfile.txt 那么为什么第一条路径工作正常而第二条路径却不起作用呢?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用 AssetManager 来访问 asset 文件夹中的文件。
You have to use AssetManager to access the files in assets folder.
所以我所做的是:我编写了一个方法,将文件从资产复制到我的应用程序的数据目录,然后将此路径发送到“非常复杂的方法”。
现在效果很好。
代码示例(也许将来会对其他人有所帮助):
///从资产文件夹中打开文件
InputStream in = getAssets().open("myfile.txt");
///创建输出及其位置
///您可以设置您喜欢的任何路径
(new File("data/data/myAppPackageName/databases/")).mkdirs();
OutputStream out = new FileOutputStream("data/data/myAppPackageName/databases/myfile.txt");
///将方法从“in”复制到“out”
作为参数发送到“非常复杂的方法”的路径:
“数据/数据/myAppPackageName/数据库/myfile.txt”
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"