Arthur Kushman
Arthur Kushman

Reputation: 3629

Android can't find the directory of a file had been put on sd card

The problem is that I put the file through the DDMS Perspective in data and /data/data folder and when I try to access to that files, the FileNotFoundException occures. Code:

                String path = Environment.getExternalStorageState()+"/analyzer_settings.xml";
                System.out.println(path);

                try {
                    Scanner in = new Scanner(new File(path));
                    while (in.hasNext()) {
                        System.out.println(in.nextLine());
                    }
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

Exception with print:

07-11 17:10:02.470: I/System.out(13506): mounted/analyzer_settings.xml
07-11 17:10:02.500: W/System.err(13506): java.io.FileNotFoundException: /mounted/analyzer_settings.xml: open failed: ENOENT (No such file or directory)

I've tried also:

String path = Environment.getExternalStorageState()+"/data/analyzer_settings.xml";
or 
String path = Environment.getExternalStorageState()+"/data/data/analyzer_settings.xml";

nothing worked for me.

Upvotes: 1

Views: 3355

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

use

String path = Environment.getExternalStorageDirectory().getAbsolutePath()
              +"analyzer_settings.xml";

instead of

 String path = Environment.getExternalStorageState()+"/analyzer_settings.xml";


EDIT: Because as doc says:

getExternalStorageDirectory ():

Gets the Android external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

and

getExternalStorageState():

Gets the current state of the primary "external" storage device.

Upvotes: 4

Venkatesh
Venkatesh

Reputation: 144

Try this coding,

       File sdcard = Environment.getExternalStorageDirectory();

    File file = new File(sdcard,"billdump1.xml");//Don't use '/' symbols

Upvotes: 1

Related Questions