Question

I am currently working on an app, and I have some data that I want to load into my app from a folder called "tests". I have created this folder in Eclipse just in my app's directory, so next to /res and /bin and so on.

Now I want to access this folder from my app. But I am not having any progress. What I did so far was getting my application's path with:

testPath = getApplicationInfo().dataDir;

But in this directory, there are only 2 subfolders called "lib" and "cache". Where do I find my "tests" folder?

Was it helpful?

Solution

You can follow like this :

public class ReadFileAssetsActivity extends Activity {

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {

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

        TextView txtContent = (TextView) findViewById(R.id.txtContent);
        TextView txtFileName = (TextView) findViewById(R.id.txtFileName);
        ImageView imgAssets = (ImageView) findViewById(R.id.imgAssets);

        AssetManager assetManager = getAssets();

        // To get names of all files inside the "Files" folder
        try {
            String[] files = assetManager.list("Files");


            loop till files.length

            {
                txtFileName.append("\n File :"+i+" Name => "+files[i]);
            }


        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // To load text file
        InputStream input;
        try {
            input = assetManager.open("helloworld.txt");

             int size = input.available();
             byte[] buffer = new byte[size];
             input.read(buffer);
             input.close();

             // byte buffer into a string
             String text = new String(buffer);

             txtContent.setText(text);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // To load image
        try {
            // get input stream
            InputStream ims = assetManager.open("android_logo_small.jpg");

            // create drawable from stream
            Drawable d = Drawable.createFromStream(ims, null);

            // set the drawable to imageview
            imgAssets.setImageDrawable(d);
        }
        catch(IOException ex) {
            return;
        }
    }
}

If you can details about this

Read file from Assets

another tutorial Open & Read File from Assets

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top