Access getExternalStorageDirectory

Using fairly simple Android code, I am trying to access the folder according to the following logic:

public void try_to_navigate_local_dir(){ Environment e = new Environment(); ArrayList<String> filesList = new ArrayList<String>(); String sd_card = Environment.getExternalStorageDirectory().toString() + "/subdir_x"; File file = new File( sd_card ) ; File list[] = file.listFiles(); for( int i=0; i< list.length; i++) { filesList.add( list[i].getName() ); } } 

Where subdir_x is being created by another application on my phone.

I can browse in these folders using the file manager, but my code returns null when I try file.listFiles() .

How can I access these files?

+5
source share
3 answers

I tried this logic in Marshmallow (6.0):

  • make sure you have this in the manifest:

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

  1. when you are trying to access external storage, please check the permission before entering the logic to read the directory:

     public void checkPermissionReadStorage(Activity activity){ if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_EXTERNAL_STORAGE)) { // Show an expanation to the user *asynchronously* -- don't block // this thread waiting for the user response! After the user // sees the explanation, try again to request the permission. } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_STORAGE); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } } } 
  2. and then override this method in your activity:

     @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case PermissionManager.MY_PERMISSIONS_REQUEST_READ_STORAGE: { //premission to read storage if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. Toast.makeText(SnapSellAddActivity.this, "We Need permission Storage", Toast.LENGTH_SHORT).show(); } return; } // other 'case' lines to check for other // permissions this app might request } } 
  3. after that you can finally read the external storage on Android devices, I tried how to read the directory in the external storage as this method:

     public File getPreviewTempExternalDirectory(Context context) { File previewDir = new File(context.getExternalFilesDir(null), PLACE YOUT DIR HERE); if (!previewDir.exists()) previewDir.mkdir(); return previewDir; } 

    and now you have a directory, you can specify a file or create a file there. hope this helps.

+7
source

Before Marshmallow, follow @Cory Charlton's answer. However, his answer for Android Marshmallow (6.0) will not work.

In fact, starting with Android 6.0 (API level 23), we no longer have the following permission.

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

For more information, please ask this question:

By the way, Environment e = new Environment(); useless delete it.

+3
source

Make sure your AndroidManifest.xml has the appropriate permission:

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

The next problem may be your file name. I use the following in one of my applications and it works (note the trailing slash and the lack of a leading slash):

 directory = new File(Environment.getExternalStorageDirectory(), "Subdirectory/") final File[] files = directory.listFiles(); 

The next problem may be that the Environment.getExternalStorageDirectory() path gives you. Newer devices have a section on internal storage that emulates an SD card, and Environment.getExternalStorageDirectory() can return this, not a real SD card.

0
source

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


All Articles