0

I'm trying to figure out how to find if a file exists in Internal Storage. My current code looks like this however when I run it in my emulator I get file not found. I believe the path of the file I am looking for is located in /storage/emulated/0/Download/filename.txt. I can see the file through the folders manager but my code does not access it. I'm not sure what I am doing wrong.

public void onClick(View v) {
                //tvRETURN.setText("File not found");
                final EditText etFilename = (EditText) findViewById(R.id.ET_filename);
                final String filename = etFilename.getText().toString();
                boolean fileFound = isFilePresent(filename);

                if(!fileFound){
                    tvRETURN.setText("File not found");
                } else{
                    tvRETURN.setText("File found");
                }

               /* try {
                    FileInputStream fileIn = openFileInput(filename);
                } catch (Exception e){
                    tvRETURN.setText("File not found");
                    e.printStackTrace();
                }*/

            }
        });
    }

    public boolean isFilePresent(String fileName) {
        String path = getBaseContext().getFilesDir().getAbsolutePath() + "/" + fileName;
        File file = new File(path);
        return file.exists();
    }
mika
  • 113
  • 1
  • 1
  • 7
  • `final String filename = etFilename.getText().toString();`. As we cannot know what the user typed in this makes no sense. – greenapps Jul 22 '15 at 16:16
  • `getFilesDir().getAbsolutePath()`. Did you inspect which path this is? It will not start with /storage/..... – greenapps Jul 22 '15 at 16:18
  • Try toasting the path string after the user input.So that you can understand the whole path or just log it out. – ShihabSoft Jul 22 '15 at 16:20
  • You should google for getexternalstoragedir and directory downloads. – greenapps Jul 22 '15 at 16:20
  • `getFilesDir()` returns `/data/data//files`. The path you want is `/storage/emulated/0/Download`, that is **external** not **internal** storage. – Jared Burrows Jul 22 '15 at 16:33
  • Thanks Jared. So I'm actually trying to get the external storage file not internal. And I have to use getDataDirectory to get the file path correct? – mika Jul 22 '15 at 17:25

0 Answers0