Need to create a folder for my Android application in Internal Storage

Viewed 198

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()
        }
1 Answers

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

Related