How to Create a folder in external sdcard in Android 4.4.2(Kitkat) API level 19 -


i try lot create folder in sd card in android 4.4.2 version. try following code

 string dirname="test";    file file = new file(environment.getexternalstoragedirectory(), dirname);         boolean status = file.mkdir();                     if (status)                         toast.maketext(mainactivity.this,                                 "directory created successfully", toast.length_short)                                 .show();                         else                             toast.maketext(mainactivity.this, "directory create failed",                                     toast.length_short).show(); 

but creates folder in internal storage.

and try code is

  string path=system.getenv("secondary_storage");                 file file=new file(path +"/test123");                 file.mkdir(); 

it creates folder in external sdcard in android 4.1 not in android 4.4.2

so how can create folder in external sdcard in android 4.4.2?? if know please me..

thanks in advance

the documentation states

applications should not directly use top-level directory, in order avoid polluting user's root namespace. files private application should placed in directory returned context.getexternalfilesdir, system take care of deleting if application uninstalled.

where top-level directory mean return value of enviroment.getexternalstoragedirectory()

using getexternalfilesdir have

 file file = new file (getexternalfilesdir(null), dirname);  if (!file.exists()) {    boolean status = file.mkdir();    if (status) {         toast.maketext(mainactivity.this, "directory created successfully", toast.length_short).show();    } else {         toast.maketext(mainactivity.this, "directory create failed", toast.length_short).show();    }  } 

also should aware of fact mkdir() returns false if directory exists


Comments

Popular posts from this blog

html - Difficulties with background-image property -

visual studio code - What does the isShellCommand property actually do and how should you use it? -

ios - Segue not passing data between ViewControllers -