在SD卡上的Andr​​oid奇巧4.4文件夹 [英] Android KitKat 4.4 folder on sd card

查看:88
本文介绍了在SD卡上的Andr​​oid奇巧4.4文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚刚倒下的犯规(EACCES权限被拒绝)

We've just fallen foul of the new permissions that apply to writing files to sd cards (external storage) on Android 4.4 (EACCES Permission Denied)

此前奇巧,我们设置了可写文件夹是这样的:

Prior to KitKat we set our writable folder like this:

mfolder = Environment.getExternalStorageDirectory().getPath() + "/appfiles";

不过搜索,我得出的结论,无论对错,关于4.4的设备,能写入文件,这需要的时间后,改为:

However after hours of searching I've come to the conclusion, rightly or wrongly that on 4.4 devices to enable writing of files this needs to be changed to:

mfolder = Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.xyz.abc/appfiles";

所以mfolder会是这样的:/mnt/sdcard/Android/data/com.xyz.abc/appfiles

So mfolder would be something like: /mnt/sdcard/Android/data/com.xyz.abc/appfiles

这是正确的,我们创建一个文件夹类似上面的SD卡,使4.4设备写入文件?

Is this correct, do we create a folder like the one above on the sdcard to enable 4.4 devices to write files?

mfolder是一个字符串,我们保存到共享preferences。

mfolder is a String that we save to shared preferences.

然后,我们有这个运行一次code如果API> = 19,改变了mfolder字符串,然后将所有的文件从旧文件夹复制到新的奇巧文件夹中。

Then we have this code that runs once if API>=19 that changes the mfolder String and then copies all the files from the old folder to the new 'kitkat' folder.

if (android.os.Build.VERSION.SDK_INT>=19){
        if (!mfolder.contains("/Android/data/com.xyz.abc/appfiles")){
            if (prefs.getBoolean("kitkatcheck", false)==false){

                //update mfolder from
                      // /mnt/sdcard/appfiles
                      // to
                      // /mnt/sdcard/Android/data/com.xyz.abc/appfiles
                String prekitkatfolder = mfolder;
                String kitkatfolder = mfolder.replace("/appfiles", "/Android/data/com.xyz.abc/appfiles");
                mfolder = kitkatfolder;
                try {
                    File sd = new File(mfolder);
                    if(!sd.exists() || !sd.isDirectory()) {
                        sd.mkdirs();
                    }
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), "Error creating Kitkat folder!\n" + e.toString(), Toast.LENGTH_LONG).show();
                    return;
                }
                prefEditor.putString("patternfolder", mfolder);
                prefEditor.putBoolean("kitkatcheck", true);
                prefEditor.commit();

                //copy files and folder from old appfiles folder to new.
                AllFiles.clear();
                listFilesAndFilesSubDirectories(prekitkatfolder);
                if (AllFiles.size()>0){
                    for (File child : AllFiles ) {
                        try {

                            File dest = new File(child.toString().replace(prekitkatfolder, kitkatfolder));


                            try {
                                String filePath = dest.getPath().substring(0, dest.getPath().lastIndexOf(File.separator));
                                File subfolder = new File(filePath);
                                if(!subfolder.exists() || !subfolder.isDirectory()) {
                                    subfolder.mkdirs();
                                }
                            } catch (Exception ex) {
                            }

                            copyFile(child, dest);  

                        } catch (Throwable t) {

                        }
                    }

                }


            }

        }

我再通知他们的文件已被复制到新的文件夹中,用户由于新的权限,他们将不得不手动删除旧的prekitkatfolder文件夹。我猜他们只能这样做,如果他们有一个股票的文件管理器,或者如果他们卸除SD卡,并将其放置在一台PC,由于新的4.4权限?

I then notify the user that their files have been copied to the new folder and that due to the new permissions they would have to manually delete the old prekitkatfolder folder. I guess they will only be able to do this if they have a stock file manager or if they unmounted sd card and place it in a PC, due to the new 4.4 permissions?

此外,对我们来说似乎是,这些4.4权限不会影响到我们所有的用户提供奇巧。一些人仍然可以写入到原来的文件夹位置上的外部存储以及一些得到EACCES(权限被拒绝)错误。任何人都可以扔,为什么这可能是任何光线,人们可能会认为它会使用外部存储适用于所有4.4的设备?

Also, for us it appears that these 4.4 permissions are not affecting all our users with Kitkat. Some can still write to the original folder location on their external storage and some get the EACCES (Permission Denied) error. Can anyone throw any light on why this might be, one would think it would apply to all 4.4 devices using external storage?

因为我们有我们有测试使用模拟器(API 19)本code无实际4.4的设备,但我们没有得到EACCES权限被拒绝的错误。因此,我们发布了一个测试版与code以上,并已告知,复制文件中内部存储结束了,这怎么可能?

As we have no actual 4.4 device we are having to test this code using the emulator (API 19) but we do not get the EACCES Permission Denied error. So we released a beta version with code above and have been told that the copied files ended up in internal storage, how can that be?

提前任何想法,我们在做什么错了,谢谢

Any ideas what we're doing wrong, thanks in advance

推荐答案

更​​新的解决方案。

此设置并创建在正确的地方奇巧的文件夹。

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

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

然而,这不是完整的证明,如果Android设备既具有内部和外部二级存储位置,上述将使用内部之一。不是真的是我们想要的,我们需要的路径可移动SD卡或更好的路径,最自由的可用空间的二次storagelocation。

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);

请注意在getExternalFilesDirs年底的S。这就造成了二次外部存储位置的数组。

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...
}

这是我们通过test_mount检查possible_kitkat_mounts的数组,看看我们确实可以选择的位置,如果成功的话,我们添加位置arrMyMounts。

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.

通过整理arrMyMounts我们就可以得到位置最可用空间。

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

嘿preSTO,arrMyMounts.get(0)。名称是最可用空间的奇巧辅助存储位置。

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

这篇关于在SD卡上的Andr​​oid奇巧4.4文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆