Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating app's custom folder in internal storage root path without any permissions #108

Closed
maq796113 opened this issue Dec 31, 2022 · 3 comments

Comments

@maq796113
Copy link

Library version: 1.5.3
OS version: Android 11
Device model: Infinix X693

Describe the bug
So basically my main objective is to gain access to the Internal Directory "storage/emulated/0" and create a new directory with my Android App's name. The app will be storing some audio files there. I have attached the code (Github Gist) that I used. I made use of SimpleStorageHelper that helps me get the access to a specific folder in the internal storage. But I want access to the whole root so that I can make my directory there. Focus on the createAudioFolder() function at line 42 where I use PublicDirectory() to get DocumentFile parent. I am also trying to get absolute path to the folder which I will later use for MediaRecorder. I have to pass a path to the setOutputFile() as an argument which will create an audio file at line 116.

In the video, you can see storageHelper.requestStorageAccess() is called , it asks for access to the files but then it crashes and it reverts back to the original Login Activity.
https://user-images.githubusercontent.com/67544354/210143938-f11fb483-8b97-489e-ac96-876c04708ff3.mp4

https://gist.github.com/maq796113/a1757542fb5891def3d4285cdb044a6c

Stacktrace
image

@anggrayudi
Copy link
Owner

anggrayudi commented Dec 31, 2022

I want access to the whole root

I understand that you want to create a folder without prompt any dialogs to the user, i.e. /storage/emulated/0/YourAppName, but you can't do that since API 30 without full disk access (android.permission.MANAGE_EXTERNAL_STORAGE).

Even WhatsApp is no longer able to create custom directory like /storage/emulated/0/WhatsApp since Android 12 (reference: #107 (comment)). Thus requesting root access is not a future-proof solution. The best solution is by writing your audio files inside your app's private directory, for example:

  • context.getExternalMediaDirs() will return /storage/emulated/0/Android/media/<app-package> (current WhatsApp solution)
  • context.getExternalFilesDir(Environment.DIRECTORY_RECORDINGS) will return /storage/emulated/0/Android/data/<app-package>/files/Recordings

Those paths do not require any storage permissions.

it crashes and it reverts back to the original Login Activity

You're trying to create an enum from string /storage/emulated/0, which is not available in the enum constants. Use available constants from this file: PublicDirectory.kt, for example PublicDirectory.MUSIC, PublicDirectory.DOWNLOADS, etc.

@anggrayudi anggrayudi changed the title Poor Documentation, How Can I Make A Folder In The Internal Directory Creating app's custom folder in internal storage root path without any permissions Dec 31, 2022
@maq796113
Copy link
Author

Can you give me an example? How should I go about implementing it?

@anggrayudi
Copy link
Owner

Here's the example:

val input = URL("https://google.com/audio.mp3").openStream()
val folder = externalMediaDirs.first()
val file = File(folder, "recitation.mp3")
val output = file.outputStream()
// will be located in /storage/emulated/0/Android/media/<app-package>/recitation.mp3
output.write(input.read())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants