-1

I must transfer a data file necessary for my app. I read many threads on the subject and I stll don't understand how it works. 1. I'm using android studio 0.8.6. A lot of threads mentions the folder assets which apparently resides in src/main. When I create a new project the folder doesn't exist. I create manually one and I put in it jpg and txt files. 2. I run the following code:

AssetManager am = getAssets(); 
String[] files = new String[0]; 
try { 
files = am.list("Files ; 
} catch (IOException e) { 
e.printStackTrace(); 
} 
for(int i=0;i<files.length;i++){ 
Toast.makeText(getApplicationContext(), "File: "+files[i]+" ", Toast.LENGTH_SHORT).show(); 
}

And I get a files.length = 0 1. I can create files, write in it and read it but I don know where they reside. And that's not what I want to do. I want to pass the data with the app. Sorry for the long email but I'm lost. Thanks in advance!

narb
  • 958
  • 1
  • 13
  • 39

2 Answers2

0

The code I have used to read files from assets is listed below:

public String ReadFromfile(String fileName, Context context) {
    StringBuilder returnString = new StringBuilder();
    InputStream fIn = null;
    InputStreamReader isr = null;
    BufferedReader input = null;
    try {
        fIn = context.getResources().getAssets()
                .open(fileName, Context.MODE_WORLD_READABLE);
        isr = new InputStreamReader(fIn);
        input = new BufferedReader(isr);
        String line = "";
        while ((line = input.readLine()) != null) {
            returnString.append(line);
        }
    } catch (Exception e) {
        e.getMessage();
    } finally {
        try {
            if (isr != null)
                isr.close();
            if (fIn != null)
                fIn.close();
            if (input != null)
                input.close();
        } catch (Exception e2) {
            e2.getMessage();
        }
    }
    return returnString.toString();
}

This code is not mine and can be found in the answers below:

read file from assets

Community
  • 1
  • 1
hafridi
  • 258
  • 3
  • 9
0

I did some progress using the following code:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        File dir = getFilesDir();
        File f = new File("/data/data/com.example.bernard.myapp/");

        File file[] = f.listFiles();
        for(int i=0;i<file.length;i++){
            Toast.makeText(getApplicationContext(), "File: "+String.valueOf(file[i]), Toast.LENGTH_SHORT).show();
        }

    }

I code in hard what seems to me like the root of my app:

File f = new File("/data/data/com.example.bernard.myapp/");

The result is I can see 3 files: lib, cache, files in "files" appears the files I create running the app. I still don't know where is assets, neither where I transfer/put the .txt and .jpg files I want to use with my app. I develop using studio.

Pang
  • 9,564
  • 146
  • 81
  • 122
narb
  • 958
  • 1
  • 13
  • 39