Android KitKat 4.4 folder on sd card

妖精的绣舞 提交于 2019-11-27 21:20:12

Updated solution.

This sets and also creates the folder in the correct place for KitKat.

mfolder = this.getExternalFilesDir("asubfoldername").getAbsolutePath();

However, this isn't full-proof, if the Android device has both an internal and external secondary storage locations, the above will use the internal one. Not really what we want as we require path to removable sdcard or better still the path to the secondary storagelocation with the most free available space.

File[] possible_kitkat_mounts = getExternalFilesDirs(null);

Note the "s" on the end of getExternalFilesDirs. This creates an array of secondary external storage locations.

for (int x = 0; x < possible_kitkat_mounts.length; x++) {
    //Log.d("test", "possible_kitkat_mounts " + possible_kitkat_mounts[x].toString());
    boolean isgood=false;
    if (possible_kitkat_mounts[x] != null){
        isgood = test_mount(possible_kitkat_mounts[x].toString());  
        if (isgood==true){
            arrMyMounts.add(newymounts(Device_get_device_info(possible_kitkat_mounts[x].toString()), possible_kitkat_mounts[x].toString()));
        }   
    }
}                           

//sort arrMyMounts size so we can use largest
Collections.sort(arrMyMounts, new Comparator<mymounts>(){
    public int compare(mymounts obj1, mymounts obj2){
        return (obj1.avaliablesize > obj2.avaliablesize) ? -1: (obj1.avaliablesize > obj2.avaliablesize) ? 1:0 ;
    }
}); 


if (arrMyMounts.size()>0){
    mfolder = arrMyMounts.get(0).name + "/asubfoldername";
    //Log.d("test", "selected kitkat mount " + kitkatfolder);   
}else{
    //do something else...
}

From the array of possible_kitkat_mounts we check via test_mount to see if we can actually write to the selected location and if successful we add that location to arrMyMounts.

By sorting arrMyMounts we can then get the location with the most available free space.

Hey presto, arrMyMounts.get(0).name is a kitkat secondary storage location with the most free space.

Google has blocked write access to external storage devices in Android 4.4. Until they change it there is no way to revert it back without root.

More info: https://groups.google.com/forum/#!msg/android-platform/14VUiIgwUjY/UsxMYwu02z0J

It might be working on some devices with Kitkat which have minisd card slot. It is confusing :(

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!