2

I am trying to create few files in Android app-specific folder in external storage. As this is specific to my app only and not required for the user purpose so, its placed at this location.

The API that I am using is getExternalFilesDir. If I try to create the folder using mkdir at this location, it is giving me return value as -1 and errno with the value of PERMISSION_DENIED. As this API doesn't require any permission from the user, we should be able to create files and directory at this place.

This is observed from only Android's API 33 version. Is there anything changed?

Edit:

// This is the Main activity class class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding private lateinit var externalAppSpecificPath : String public external fun CreateDir (pString: String) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) // Example of a call to a native method binding.sampleText.text = stringFromJNI() externalAppSpecificPath = applicationContext.getExternalFilesDir("").toString() Log.d ("TAG", externalAppSpecificPath) // Jumping to C++ CreateDir (externalAppSpecificPath) } /** * A native method that is implemented by the 'testapp' native library, * which is packaged with this application. */ external fun stringFromJNI(): String companion object { // Used to load the 'testapp' library on application startup. init { System.loadLibrary("testapp") } } } // CPP file extern "C" JNIEXPORT void JNICALL Java_com_example_testapp_MainActivity_CreateDir(JNIEnv *env, jobject thiz, jstring p_string) { const char * string = env->GetStringUTFChars(p_string, nullptr); const char * subfolder = "/subfolder"; int newlength = strlen(string) + strlen(subfolder) + 1; char * newstring = new char [newlength]; strcpy(newstring, string); strcat(newstring, subfolder); __android_log_print (ANDROID_LOG_VERBOSE, "Tag", "New string->%s", newstring); int ret_val = mkdir ((const char *) newstring, S_IRWXU); __android_log_print (ANDROID_LOG_DEBUG, "Tag", "mkdir return value-->%d, errno-->%d", ret_val, errno); } 

enter image description here

6
  • 1
    Please show a minimal reproducible example Commented Apr 3, 2024 at 6:02
  • Can you share a code sample? Commented Apr 3, 2024 at 6:03
  • Added a minimal reproducible example Commented Apr 3, 2024 at 6:56
  • Only call mkdir if the directory does not exist yet. If it already exists mkdir will return false. Commented Apr 3, 2024 at 8:17
  • return value as -1 mkdir returns true or false. What would -1 be? You did not tell. Commented Apr 3, 2024 at 8:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.