无法创建外部SD卡的目录(棒棒堂) [英] Can't create a directory in external sd card (Lollipop)

查看:292
本文介绍了无法创建外部SD卡的目录(棒棒堂)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要奇巧与内部存储没有问题,但我想创建外部SD卡的目录,与之前的版本, File.mkdirs()

I have no problem with internal storage but I want to create a directory in external sd card, with versions prior to KitKat, File.mkdirs() works

还有就是我的code的一部分:

There is part of my code:

//external sd card
DocumentFile.fromFile(new File("/storage/sdcard1/")).exists(); //returns true
DocumentFile.fromFile(new File("/storage/sdcard1/")).canRead(); //returns true
DocumentFile.fromFile(new File("/storage/sdcard1/")).canWrite(); //returns true
DocumentFile.fromFile(new File("/storage/sdcard1/test")).exists(); //returns false
DocumentFile.fromFile(new File("/storage/sdcard1/")).createDirectory("test"); //returns null

//internal storage
DocumentFile.fromFile(new File("/storage/emulated/0/")).exists(); //returns true
DocumentFile.fromFile(new File("/storage/emulated/0/test")).exists(); //returns false
DocumentFile.fromFile(new File("/storage/emulated/0/")).createDirectory("test"); //it works
DocumentFile.fromFile(new File("/storage/emulated/0/test")).exists(); //returns true
DocumentFile.fromFile(new File("/storage/emulated/0/test")).createFile("text", "file.txt"); //it works

//external sd card
(new File("/storage/sdcard1/test2/subfolder")).exists(); //returns false
(new File("/storage/sdcard1/test2/subfolder")).mkdirs(); //returns false

//internal storage
(new File("/storage/emulated/0/test2/subfolder")).exists(); //returns false
(new File("/storage/emulated/0/test2/subfolder")).mkdirs(); //returns true

在AndroidManifest.xml中

In AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

测试与BQ Aquaris E4,Android的棒棒糖5.0和1GB的微型SD

Testing with an BQ Aquaris e4, Android Lollipop 5.0 and a 1GB micro sd

更新:这是我如何获得卷列表:

UPDATE: This is how i get the volume list:

private static ArrayList<StorageInfo> listAvaliableStorage(Context context) {
        ArrayList<StorageInfo> storagges = new ArrayList<>();
        StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
        try {
            Class<?>[] paramClasses = {};
            Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);
            getVolumeList.setAccessible(true);
            Object[] params = {};
            Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);
            if (invokes != null) {
                StorageInfo info;
                for (Object obj : invokes) {
                    Method getPath = obj.getClass().getMethod("getPath");
                    String path = (String) getPath.invoke(obj);
                    info = new StorageInfo(path);
                    File file = new File(info.getPath());
                    if ((file.exists()) && (file.isDirectory())
                        //&& (file.canWrite())
                            ) {
                        info.setTotalStorage(file.getTotalSpace());
                        info.setFreeStorage(file.getUsableSpace());
                        Method isRemovable = obj.getClass().getMethod("isRemovable");
                        String state;
                        try {
                            Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
                            state = (String) getVolumeState.invoke(storageManager, info.getPath());
                            info.setState(state);
                            info.setRemovable((Boolean) isRemovable.invoke(obj));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        storagges.add(info);
                    }
                }
            }
        } catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        storagges.trimToSize();

        return storagges;
    }

硬codeD的路径只有在这个例子

The hard coded paths is only for this example

更新2:
我创建的目录:测试中的SD卡与文件浏览器。当我执行这个code:

UPDATE 2: I've created the directory: "test" in SDCard with a file explorer. When I execute this code:

File file = new File("/storage/sdcard1/test/", "file.txt");
fileOutput = new FileOutputStream(file);

第二行抛出 FileNotFoundException异常:/storage/sdcard1/test/file.txt:打开失败:EACCES(拒绝)

我是什么做错了吗?
谢谢

What am I doing wrong? Thanks

推荐答案

这是由于到Android棒棒堂的设计。
Environment.getExternalStorageDirectory()将返回模拟外部存储的唯一路径。
Android不公开,让你的SD卡的路径上的任何API。

That is due to the design of Android Lollipop. Environment.getExternalStorageDirectory() will return only the path of emulated external storage. Android does not expose any API that gives you the path of the sdcard.

此外,棒棒堂,应用程序无法写入的位置,在SD卡自己的目录之外,除非是通过存储访问架构获得的用户权限。

Also, in Lollipop, apps cannot write to location outside of their own directory in the sdcard, unless user permission is obtained through Storage Access Framework.

尝试 context.getExternalFilesDirs()。这就给了你的路径,在您的应用程序可以将数据写入到列表中。其中的一个会在SD卡,并会像/存储/ sdcardname / Android的/数据/ yourAppName /文件

Try context.getExternalFilesDirs(). That gives you a list of paths where your app can write data to. One of it will be in the sdcard and will go like /storage/sdcardname/Android/data/yourAppName/files

这篇关于无法创建外部SD卡的目录(棒棒堂)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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