How to check if file exists in the application directory in the internal storage?

Asked

Viewed 1,496 times

0

I am trying to check if a file exists in the application directory in the internal storage with the code below

String path = Environment.getExternalStorageDirectory().getPath()+"/meu_diretorio/arquivo";
    boolean exists = (new File(path)).exists();
    if (!exists) {
        Toast.makeText(getActivity(), "O arquivo não existe!", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(getActivity(), "O arquivo existe!", Toast.LENGTH_SHORT).show();
    }

I’m using the permissions:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

However it is not working. How should I proceed with this check?

  • the meu_diretório has same accent or only example?

  • It was just an example.

  • Are you giving any error in the application?! Have you granted read or write permission?

  • I am using the permissions I included in the question. There is no error.

  • Instead of getPath() you tried to use getAbsolutePath()?

  • It didn’t work either!

  • Let me understand! There is the file, but your application does not recognize right?! You came to debug and see if you are getting the path correctly?!

  • The path that is shown in the path is storage/emulated/0/ but the right thing should be data/br.com.minhaaplicacao/meu_diretorio/arquivo or I’m confusing everything?

Show 3 more comments

1 answer

1


The code you’re using is to access the storage external.

If you want to get the path to the root of the storage intern must use File getFilesDir().
To get the full path to a file name, stored in storage intern, use File getFileStreamPath(String name)

Notes:

  • Thanks, I managed to solve the problem presented in the topic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.