0

I'm trying to solve the problem with creating directory on Android device. I noticed that some third party apps create catalog on internal directory /storage/emulated/0/Android/data which. How It can achieved? Here's my piece of code, but It doesn't work :

 if(FileUtils.isExternalStorageAvailable() && FileUtils.isExternalWritable()){
                File path = new File(Environment.getExternalStorageDirectory() +"/Android/data/reports");
                Log.d("DEBUG", Environment.getExternalStorageDirectory() +"/Android/data/reports");
                if(path.mkdirs()){
                    Toast.makeText(getActivity(), "catalog created", Toast.LENGTH_LONG).show();
                }
            }

Thanks For Help, Regards

adams172
  • 189
  • 1
  • 10
  • 1
    Please explain, **in detail**, what "It doesn't work" means and **exactly** how you determined that "It doesn't work". – CommonsWare Dec 05 '16 at 20:24
  • Sorry for being imprecise, I mean that it just doesn't create a catalog I don't receive any error. I also tried to create catalog directly in /storage/emulated/0 and e.g storage/emulated/0/Picutres, result is the same. In the other hand I am allowed to create the catalog on internal storage but it isn't available for other applications from there, Adobe Reader to be precise. – adams172 Dec 05 '16 at 20:54
  • "it just doesn't create a catalog" -- please explain, **in detail**, what this means and how you tested it. For example, if by "catalog" you mean "directory", are you checking using `adb shell ls`, or an on-device file manager, or a desktop OS's drive letter/mounted volume, or something else? – CommonsWare Dec 05 '16 at 21:02
  • Ok, so here's what showed up in logcat when I tried to create directory: 12-05 16:08:49.410 11413-11413/com.steveq.cashcontrol D/DEBUG: External storage available and writable 12-05 16:08:49.411 11413-11413/com.steveq.cashcontrol D/DEBUG: /storage/emulated/0/Android/data/reports 12-05 16:08:51.324 11413-16620/com.steveq.cashcontrol E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe00d3310 I also check in Android Device Monitor if the file was created. I'd like to mention that I added WRITE_EXTERNAL_STORAGE to the manifest. – adams172 Dec 05 '16 at 21:09

1 Answers1

0

You may be being blocked because Android/data/ on external storage is used by Android for getExternalFilesDir() and kin. If you want a directory in Android/data/, just use getExternalFilesDir() (bonus: no permission required on API Level 19+).

Beyond that, make sure that you are handling runtime permissions.

And, eventually, you will want to add code to allow users to see files that you put in your directory.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • When I use **getExternalFilesDir()** I get **/storage/emulated/0/** and in this directory I can't create my folder also then I tried use **Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + /reports** where _"reports"_ is my directory, the catalog is not created, even **Documents** directory is not visible in Android Device Monitor – adams172 Dec 05 '16 at 21:42
  • @adams172: "When I use getExternalFilesDir() I get /storage/emulated/0/" -- `getExternalFilesDir()` on `Context` should not return `/storage/emulated/0/`. "even Documents directory is not visible in Android Device Monitor" -- that directory may not be created automatically, particularly depending on the version of Android that you are running. You might consider using other tools (e.g., `adb shell ls`) to confirm what you are seeing in the Monitor. In my environment, the file manager in the Monitor has not worked in some months, though it my case it shows nothing at all. – CommonsWare Dec 05 '16 at 21:48
  • I checked via adb shell and directory hasn't been created. It looks like I am not allowed to create the directory there because when I try to do that in Internal storage **context.getFilesDir()** it works fine, but It dosen't solve my problem due to I need to share files from the directory with other app - AdobeReader. – adams172 Dec 05 '16 at 21:54
  • @adams172: "I need to share files from the directory with other app - AdobeReader" -- well, you can't do that on Android 7.0+ with external storage either, once you raise your `targetSdkVersion` to 25. Use `FileProvider` to be able to keep the files on internal storage yet make them available to third-party apps. – CommonsWare Dec 05 '16 at 21:56