Cant Create Folder in Android Q with mkdir
Asked Answered
M

2

8

My app create folder in all android version without problem, except android Q , i don't know why my code not work in android q

Here is my code:

File dir = new File("/sdcard/test");
       try{
              if(dir.mkdir()) {
                      Log.d("DOWNLOAD","Directory created");
              } else {
                     Log.d("DOWNLOAD","Directory is not created");
              }
            }catch(Exception e){
                  e.printStackTrace();
        }
Mahmud answered 5/7, 2019 at 7:44 Comment(9)
what is in the logcat ?Ree
Please have a look developer.android.com/preview/privacy/scoped-storage Google has introduced Scoped Storage in android QMussman
@AhmadAyyaz tnx but my english is weak I can not figure out what to do, can you tell me what should I do?Mahmud
@RahulKhurana There is nothing in logcatMahmud
my english is weak this part of StackOverflow is English-onlyPenland
I mean that we probably won't be able to help, as we can only help in EnglishPenland
@VladyslavMatviienko i know, but i cant read full document in developer.android I'm confused I just want to create a folderMahmud
and how you will learn if you cannot read full document?Nowlin
There is nothing in the logcat as because you haven't put anything like Log.e("DOWNLOAD","Exception: ".e.toString()); in your catch block. Please avoid using printStackTrace().Haldane
P
6

In Q, if you are wanting to access a file that is not a common music or media file, i.e. in your case sdcard/test then you need to do the below:

In order to access any other file that another app has created, including files in a "downloads" directory, your app must use the Storage Access Framework, which allows the user to select a specific file.

Quote src: http://developer.android.com/preview/privacy/scoped-storage

// Here are some examples of how you might call this method.
// The first parameter is the MIME type, and the second parameter is the name
// of the file you are creating:
//
// createFile("text/plain", "foobar.txt");
// createFile("image/png", "mypicture.png");

// Unique request code.
private const val WRITE_REQUEST_CODE: Int = 43
...
private fun createFile(mimeType: String, fileName: String) {
    val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
        // Filter to only show results that can be "opened", such as
        // a file (as opposed to a list of contacts or timezones).
        addCategory(Intent.CATEGORY_OPENABLE)

        // Create a file with the requested MIME type.
        type = mimeType
        putExtra(Intent.EXTRA_TITLE, fileName)
    }

    startActivityForResult(intent, WRITE_REQUEST_CODE)
}

Example src https://developer.android.com/guide/topics/providers/document-provider

Paratroops answered 5/7, 2019 at 9:26 Comment(1)
what is mimetype for folder?Mahmud
D
10
 <manifest ... >
 <!-- This attribute is "false" by default on apps targeting Android Q. ->
  <application android:requestLegacyExternalStorage="true" ... >

 </application>
</manifest>
Distraint answered 21/12, 2019 at 8:6 Comment(2)
I think it's worth mentioning that this is just a hotfix and that it will not work once Android R comes into the picture!Bona
I got rid of this problem thanks to you. Thanks. (for Android 10)Salesman
P
6

In Q, if you are wanting to access a file that is not a common music or media file, i.e. in your case sdcard/test then you need to do the below:

In order to access any other file that another app has created, including files in a "downloads" directory, your app must use the Storage Access Framework, which allows the user to select a specific file.

Quote src: http://developer.android.com/preview/privacy/scoped-storage

// Here are some examples of how you might call this method.
// The first parameter is the MIME type, and the second parameter is the name
// of the file you are creating:
//
// createFile("text/plain", "foobar.txt");
// createFile("image/png", "mypicture.png");

// Unique request code.
private const val WRITE_REQUEST_CODE: Int = 43
...
private fun createFile(mimeType: String, fileName: String) {
    val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
        // Filter to only show results that can be "opened", such as
        // a file (as opposed to a list of contacts or timezones).
        addCategory(Intent.CATEGORY_OPENABLE)

        // Create a file with the requested MIME type.
        type = mimeType
        putExtra(Intent.EXTRA_TITLE, fileName)
    }

    startActivityForResult(intent, WRITE_REQUEST_CODE)
}

Example src https://developer.android.com/guide/topics/providers/document-provider

Paratroops answered 5/7, 2019 at 9:26 Comment(1)
what is mimetype for folder?Mahmud

© 2022 - 2024 — McMap. All rights reserved.