Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a file from assets folder of my android test project?

Is there a way to get the file object of one of the files which are inside the assets folder. I know how to load the inputstream of such a file, but i need to have a file object instead of the inputstream.

This way i load the inputstream

    InputStream in2 = getInstrumentation().getContext().getResources().getAssets().open("example.stf2");

But I need the file object, this way the file will not be found

File f = new File("assets/example.stf2");
like image 447
Al Phaba Avatar asked Apr 29 '14 09:04

Al Phaba


1 Answers

Found a soltion which works in my case, mabye someone else can use this as well.

Retrieving the file from my android test project to an inputstream

InputStream input = getInstrumentation().getContext().getResources().getAssets().open("example.stf2");

Create a file on the External-Cachedir of the android application under test

File f = new File(getInstrumentation().getTargetContext().getExternalCacheDir() +"/test.txt");

Copy the inputstream to the new file

FileUtils.copyInputStreamToFile(input, f);

Now I can use this file for my further tests

like image 56
Al Phaba Avatar answered Oct 08 '22 01:10

Al Phaba