How to read from internal storage (subfolder Download)?

After an intensive search, I could not find out how to read the configuration file (xml) from internal memory (loading a subfolder). I tried to access /mnt/sdcard/Download/config.xml but got an exception.

When I try to get accessible folders from two possible providers ApplicationContext and Environment , I get the following data:

 context.getFilesDir().getAbsolutePath(); // /data/data/com.xxx.yyy/files context.getDir("Download", 0).getAbsolutePath(); // /data/data/com.xxx.yyy/app_Download context.getDir("Download", 1).getAbsolutePath(); // /data/data/com.xxx.yyy/app_Download Environment.getExternalStorageDirectory().getAbsolutePath(); // /storage/emulated/0 context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(); // /storage/emulated/0/Android/data/com.xxx.yyy/files/Download Environment.getDataDirectory().getPath(); // /data 

When I try to get this information to access config.xml in the Download- folder (I manually copied the file through Windows Explorer), I get errors, for example. FileNotFoundException or IllegalArgumentException exception.

Where is my file and how can I access it properly?

0
source share
2 answers
  • Set read permission in your AndroidManifest.xml:

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
  • Use the following code:

     String filename = Environment.getExternalStorageDirectory().getPath() + File.separator + "download" + File.separator + "config.xml"; 
  • Use filename for what you want to do.
+2
source

When you try to view a file from a subfolder in the internal storage, you can try to execute the code. Sometimes you may encounter an openFileInput whey problem trying to convey context. here is the function.

  public String getDataFromFile(File file){ StringBuilder data= new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(file)); String singleLine; while ((singleLine= br.readLine()) != null) { data.append(singleLine); data.append('\n'); } br.close(); return data.toString(); } catch (IOException e) { return ""+e; } } 
+1
source

Source: https://habr.com/ru/post/1380587/


All Articles