3

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() } 
5
  • Have you read through the current docs? 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 Commented May 16, 2022 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. Commented May 17, 2022 at 5: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 Commented May 17, 2022 at 18:32
  • @S.B. you got any solution on this? Commented Sep 22, 2022 at 5:27
  • 1
    @sasikumar, no, I created a directory in 'Downloads' instead. Commented Sep 23, 2022 at 6:06

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.