Office Hours Transcript: 2021-09-11

*             Abhishek joined

Mark_Murphy   hello, Abhishek!

Mark_Murphy   how can I help you today?

Abhishek      Hey Mark, I hope you’re doing well.

Mark_Murphy   I’m OK – how about you?

Abhishek      I’m doing good.

Abhishek      I have one issue in my project and the issue is related to scoped-storage

Abhishek      In one of our application, we’re storing some content inside /MUSIC directory using Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)

Abhishek      Everything works well below Android-11 but when we’re targetting Android-11, we’re not able to store any content. The problem is that, we’re able to create directories inside MUSIC directory but in those created directory, we’re unable to create any file using createNewFile api.

Abhishek      I know that this is all because of scoped-storage. So my question is what should we do to create file inside any default directory?

Mark_Murphy   I am surprised that you can create directories there

Mark_Murphy   for music, you probably should try using MediaStore

Abhishek      To my surprise, I’m able to create directories even after targetting 11.

Mark_Murphy   I covered this in Elements of Android Q, though my example was for videos rather than music

Mark_Murphy is the sample project

Abhishek      thanks for sharing the lead Mark.

Abhishek      I have one more thing…

              What would you suggest me to do if I can go with SAF to create file inside default directory and I can also create same file in app-storage?

Abhishek      What should I prefer and why

Mark_Murphy   for something like media, particularly if the user should be able to use the media outside of your app, I would use the appropriate directories, albeit via MediaStore on Android 11+

Mark_Murphy   so, your current approach seems fine

Abhishek      current approach as in?

Mark_Murphy   Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) on Android 10 and below

Abhishek      and what about android11+

Mark_Murphy   use MediaStore

Abhishek      main problem is when I target 11

Mark_Murphy   with MediaStore, you can write content to the same location that you are doing today, just not using File

Abhishek      in the gitlab link above, are you using MediaStore for writing content for 11+?

Mark_Murphy   yes, though in my case it is a video, not music

Abhishek      Any lead on why I’m able to create directories still?

Mark_Murphy   as I wrote, I am surprised that it works

Abhishek      LOL. Yeah!

Abhishek      using MediaStore, how can we create nested directories and then in inner-most directory I wanna create a file?

Abhishek      It seems to be a very silly question

Mark_Murphy   when you specify RELATIVE_PATH, AFAIK, that can have multiple directories in it

Mark_Murphy   so, for example, I use: put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/ConferenceVideos")

Mark_Murphy   that will create a ConferenceVideos/ directory inside of the standard Movies/ directory

Mark_Murphy   AFAIK, I could have "Movies/Hey/We/Can/Have/Many/Directories/" there instead, and it would create the whole tree

Abhishek      Nice!

Mark_Murphy   admittedly, I have not tried this, so I cannot guarantee it, but particularly for music, it should be supported

Abhishek      And which api can we use to create a file and write content in it?

Mark_Murphy   well, with MediaStore, you call insert() and get a Uri back

Mark_Murphy   you then use ContentResolver and openInputStream() to write content to that location

Mark_Murphy   again, this is covered in Elements of Android Q, which covers the example that I linked to

Abhishek      Sounds good.

Abhishek      I’ll go through the examples then.

Abhishek      Thanks Mark.

Mark_Murphy   happy to help!

Abhishek      I’m done for today. I’ll go through the examples and if find any difficulty. Will conenct again. Thanks a ton.

Mark_Murphy   👍

Abhishek      Hope, transcript will be uploaded.

Mark_Murphy   yes, I will post it to shortly after the end of the chat

*             Abhishek left