How to get the path of file in android project

3.7k views Asked by At

I have a project in android eclipse, and it will be run on android emulator. In my project, I put some xlf (xml format) files in the folder translations. And I will read them and parse. But it seems I always fail. The structure of my project is like attachment image enter image description here

And TC01_OSNdemo.java is my main package. In it, I wrote the codes to parse xml files from "translations" folder. But it seems it fail no matter in absolute path, relative path, etc...

My method1: relative path

String FileName = "./translations/myosn.mofile.android.zn_CN.xlf"
File tfile = new File(FileName);
                        if (tfile.exists())
                        {
                            System.out.println("zhaotest File exist");
                        }
                        else
                        {
                            System.out.println("zhaotest File Not Exist");
                        }

It seems it can't find the file

My method 2. absolute path in my project.

String FileName = "C:\\Users\\****\\workspaceforandroid\\OSNdemo\\translations\\myosn.mofile.android.zn_CN.xlf";

It seems it can't find the file.

My method 3.

String FileName = Environment.getExternalStorageDirectory()+ File.separator+ "OSNdemo" +File.separator + "translations"+File.separator + "myosn.mofile.android.zn_CN.xlf";

It seems it can't find the file. :(

Could anyone help me about this issue?

Thanks a lot!

1

There are 1 answers

0
Naveen Tamrakar On BEST ANSWER
assets/

You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.

Android Projects structure