Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get right path files in android

Tags:

java

android

My code is following::

Scanner sc = null;
try {
    sc = new Scanner(new File("assets/mainmenu/readSourceFile.txt"));
} catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    System.out.println(e1.toString());
    e1.printStackTrace();
}

Then, logcat show exception java.io.FileNotFoundException

How can I find my file? I tried

sc = new Scanner(new File("mainmenu/readSourceFile.txt"));

However, it's still throw FilenotFoundException

My file is in folder assets/mainmenu/readSourceFile.txt

and I've try this::

private InputStream is;
try {
        is = this.getResources().getAssets().open("mainmenu/readSourceFile.txt");
    } catch (IOException e) {
        e.toString();
    }

I use getAssets to access assets file in android. However, how can I suppose to read text file if I use InputStream

like image 742
barssala Avatar asked Dec 21 '12 04:12

barssala


People also ask

What is Android path?

android.graphics.Path. The Path class encapsulates compound (multiple contour) geometric paths consisting of straight line segments, quadratic curves, and cubic curves. It can be drawn with canvas.


1 Answers

You try this...Hope it will work

sc = new Scanner(new File("file:///android_asset/mainmenu/readSourceFile.txt");

Edit try this

AssetFileDescriptor descriptor = getAssets().openFd("mainmenu/readSourceFile.txt");
FileReader reader = new FileReader(descriptor.getFileDescriptor());

Copied from here Asset folder path

like image 129
vnshetty Avatar answered Oct 02 '22 18:10

vnshetty