2

I am creating an android application where I need to create a folder in Internal Storage to store the files generated by user (Just like how whatsapp creats one in Internal storage). I was using MANAGE_EXTERNAL_STORAGE permission but google has rejected my app because of it, and no other way is working for that. If there is any other permission or way of doing this it will be very helpful.

The File.mkdirs() is not creating folder.

I have tried other ways as mentioned here, but none of them work. Android- Saving Internal Storage to my created folder

How to create application specific folder in android gallery?

File.mkdirs() is giving false for that.

My code:

AndroidManifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

Requesting for permissions:

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                            val intent =Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                            val uri = Uri.fromParts("package", packageName, null)
                            intent.data = uri
                            startActivity(intent)
                        }
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                            val intent =Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
                            val uri = Uri.fromParts("package", packageName, null)
                            intent.data = uri
                            startActivity(intent)
                        }
                        val permissions: Array<String> = arrayOf(
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission_group.STORAGE,
                            Manifest.permission.MANAGE_DOCUMENTS,
                            Manifest.permission.INSTALL_PACKAGES
                        )
                        //requestPermissions(permissions, PackageManager.PERMISSION_GRANTED)
                        requestPermissions(permissions, 1)

Creating folder in internal storage:

        val folder = "/storage/emulated/0/"+
                getString(R.string.foldername)
        val path = folder + File.separator.toString() + getString(R.string.lbl_Order)+orderModelClass!!.name+ ".pdf"
        if (!File(folder).exists()) {
            File(folder).mkdirs()
        }
S. B.
  • 186
  • 1
  • 12
  • Have you read through the current docs? https://developer.android.com/training/data-storage If you just want to write files to internal storage, in a folder specific to your app, the system already provides one for you - you can call ``getFilesDir()`` and do what you want in there, no permissions required – cactustictacs May 16 '22 at 18:57
  • I have tried getFilesDir() but the folder is not visible in my file manager. I want the user to easily locate the folder. – S. B. May 17 '22 at 05:50
  • 2
    Yeah the new Scoped Storage framework puts limitations on that kind of thing, and it's mandatory since Android 11 (you could opt out in 10). You're not supposed to be able to poke around the filesystem like that anymore - `MANAGE_EXTERNAL_STORAGE` is a workaround for apps like file managers that *have to* be able to do that kind of thing to fulfil their primary function, but they're manually vetted on the Play Store to make sure they truly need it. You could try ``getExternalFilesDir()`` and see if that's visible to your file manager - but ideally the user wouldn't require one to do whatever – cactustictacs May 17 '22 at 18:32
  • @S.B. you got any solution on this? – sasikumar Sep 22 '22 at 05:27
  • 1
    @sasikumar, no, I created a directory in 'Downloads' instead. – S. B. Sep 23 '22 at 06:06

1 Answers1

0

I think what you have mentioned above is 'app-specific storage' which can be queried by'filesDir.path'

You don't need any permission to access that. And you can create a folder there as follows.

    private fun createFileInInternalFolder() {
    val internalFolderPath = this.filesDir.path + "/My_FOLDER"
    val fileName = File(internalFolderPath, "myFile.txt")
    if (!fileName.exists()) {
        if(fileName.createNewFile()) {
            Toast.makeText(this, "File Created", Toast.LENGTH_SHORT).show()
        }
    }
}

Further more above folder wont be visible in File browsers. Yet you can see that through Android Studio Device File Explorer

Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45