android - 关于5.0手机外置SD卡写入数据的问题
天蓬老师
天蓬老师 2017-04-18 09:04:47
0
5
541

要将视频缓存到SD卡,有的手机可以写入有的手机不可以,比如HTC D826W就无法写入
下面是我在HTC D826W上的测试代码:

findViewById(R.id.btn_create_file).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            File[] externalFiles = getExternalFilesDirs(null);
            if(externalFiles.length > 1 && externalFiles[1] != null){
                File parent = externalFiles[1];
                Log.d(TAG, parent.canWrite() ? "可以写入" : "不可写入");
                try {
                    File newFile = new File(parent, System.currentTimeMillis()+"");
                    boolean createResult = newFile.createNewFile();
                    Log.d(TAG, createResult ? "创建文件成功" : "创建文件失败");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });

上面的代码会报这个错:

D/MainActivity: 可以写入
W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
W/System.err: at java.io.File.createNewFile(File.java:941)
W/System.err: at com.xfan.writesdcard.MainActivity$2.onClick(MainActivity.java:54)
W/System.err: at android.view.View.performClick(View.java:4785)
W/System.err: at android.view.View$PerformClick.run(View.java:19869)
W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:155)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5702)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at java.io.File.createNewFile(File.java:934)
W/System.err: ... 11 more

权限也申请了:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Android4.4之后在/SD卡/Android/data/包名/ 的文件夹下可以应该是可以写入数据的呀,大部分设备是没有问题的,有些设备会出现这个问题,google了很长时间了一直不知道怎么解决,而相同的手机优酷视频却可以做到,希望有相关经验的大神指点一下,谢谢了

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(5)
Ty80

This problem is related to the system version. I have also encountered similar problems. Especially after 6.0, the Android system classifies the WRITE_EXTERNAL_STORAGE permission as a dangerous permission. Unless the user manually requests it, even if it is applied, it cannot write to the SD card. .

It is recommended that you handle it this way:
Change the file storage path to /data/data/package name/cache/; to solve this problem, because this path is a built-in path, and in the 6.0 system (API > 23), there is no need to apply for permissions You can write files to this directory.

Disadvantages: When the user uninstalls the APP or manually clears the cache in the system, the contents of this directory will also be cleared.

小葫芦

It should be a problem with the mobile phone system itself. Many mobile phone manufacturers randomly modify the system, resulting in many inexplicable bugs. It is recommended to try changing the folder to store the data.

迷茫

As for the mobile system version, API 23 or above may require you to manually request permission

刘奇

CallingFile#createNewFile()之前先得确保目录已经存在, 如果不存在, 就必须先File#mkdir()或者File#mkdirs()

Apply permission portal: Interpretation of Android official development guidelines - runtime permissions

Ty80

I made some progress today. When my application couldn't write files, I uninstalled Youku Video, and then installed Youku again but it still couldn't download the video and prompted an SD card exception. Then I inserted and unplugged the SD card and the application I used after that. Youku can be downloaded, so I infer that this problem should be a problem with the mobile phone system, not my application

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!