写入SD卡总是失败 [英] Writing to SD Card always failing
问题描述
我正在尝试从任何位置(包括内部设备存储)移动文件 到SD卡
I am trying to move files from any location (including internal device storage) to SD Card,
为此我有
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在我的清单中,并通过检查是否使用授予权限来检查它是否有效:
in my manifest, and checked that it did work by checking if premissions are granted using :
Contex.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE")
和Contex.checkCallingOrSelfPermission("android.permission.READ_EXTERNAL_STORAGE")
我尝试了3种不同的方法:
and I have tried 3 diffrent ways to do that :
- Files.move(原始版本,newPath1),是Guava com.google.common.io随附的
-
此方法:
- Files.move(original, newPath1), which comes with Guava com.google.common.io
this method :
public static void move(File src, File dst) throws IOException {
FileInputStream inStream = new FileInputStream(src);
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
}
original.renameTo(newPath1);
我阅读了很多有关此的SO帖子,并且尝试的所有解决方案均无效,我确保已安装SD卡,并且尝试时未将设备连接到PC.我什至尝试了2种带有SD卡的不同设备.请注意,我可以使用BitmapFactory.decode()使用完全相同的路径访问图像,并且可以将该图像加载到imageview.
I read alot of SO posts about this, and all solutions i tried did not work, I made sure the SD Card is mounted, and that i didnt have my device connected to PC while trying. I even tried 2 diffrent devices with SD Cards. Notice that i could access the image with BitmapFactory.decode() with the exact same path and i could load that image to imageview.
我总是收到EACCES(权限被拒绝).我不知道该怎么办:(
I am always getting EACCES (Permission denied). I dont know what else to do to solve this :(
11-13 11:11:54.358 19192-19372/xaday.ofek.ron.xaday W/System.err: java.io.FileNotFoundException: /storage/sdcard1/DCIM/thai2/IMG-20150702-WA0001.jpg: open failed: EACCES (Permission denied)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err: at libcore.io.IoBridge.open(IoBridge.java:465)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err: at com.google.common.io.Files$FileByteSink.openStream(Files.java:245)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err: at com.google.common.io.Files$FileByteSink.openStream(Files.java:233)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err: at com.google.common.io.ByteSource.copyTo(ByteSource.java:248)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err: at com.google.common.io.Files.copy(Files.java:458)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err: at com.google.common.io.Files.move(Files.java:673)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err: at xaday.ofek.ron.xaday.XaDayIntentService.handleActionMove(XaDayIntentService.java:128)
11-13 11:11:54.369 19192-19372/xaday.ofek.ron.xaday W/System.err: at xaday.ofek.ron.xaday.XaDayIntentService.onHandleIntent(XaDayIntentService.java:84)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: at android.os.Looper.loop(Looper.java:211)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: at libcore.io.Posix.open(Native Method)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: at libcore.io.IoBridge.open(IoBridge.java:451)
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: ... 12 more
11-13 11:11:54.370 19192-19372/xaday.ofek.ron.xaday W/System.err: java.io.FileNotFoundException: /storage/sdcard1/DCIM/thai2/IMG-20150702-WA0001.jpg: open failed: EACCES (Permission denied)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at libcore.io.IoBridge.open(IoBridge.java:465)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at xaday.ofek.ron.xaday.FileUtils.move(FileUtils.java:36)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at xaday.ofek.ron.xaday.XaDayIntentService.handleActionMove(XaDayIntentService.java:136)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at xaday.ofek.ron.xaday.XaDayIntentService.onHandleIntent(XaDayIntentService.java:84)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at android.os.Looper.loop(Looper.java:211)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at libcore.io.Posix.open(Native Method)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: at libcore.io.IoBridge.open(IoBridge.java:451)
11-13 11:11:54.371 19192-19372/xaday.ofek.ron.xaday W/System.err: ... 9 more
只是为了确保我没有以某种方式锁定文件,所以我使用了另一个文件管理器应用程序来执行相同的文件移动-并且它起作用了!为什么对我不起作用?!
Just to make sure I was not locking the file in some way, I used another file manager app to execute the same file move - and it worked! why is it not working for me?!
有人有主意吗?
推荐答案
在Android Kitkat(4.4)上,Google更改了开发人员访问可移动存储(SD卡)的方式.引用自 source.android.com :
On Android Kitkat (4.4), Google has changed how the developers can access to the removable storage (SD card). Quoting from source.android.com:
WRITE_EXTERNAL_STORAGE权限只能授予对 设备上的主要外部存储.应用程序不得被允许 写入辅助外部存储设备,但不包括它们 综合权限允许的特定于软件包的目录. 以这种方式限制写操作可确保系统可以清理文件 卸载应用程序时.
The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions. Restricting writes in this way ensures the system can clean up files when applications are uninstalled.
因此,即使具有WRITE_EXTERNAL_STORAGE
权限,也无法在KitKat和更高版本的SD卡上写入.
For this reason, even with the WRITE_EXTERNAL_STORAGE
permission, you can't write on the SD card on KitKat and higher versions.
在Android Lollipop(5.0)上,Google添加了一组新的API,允许开发人员通过
On Android Lollipop (5.0), Google adds a new set of API that allows the developers to manage the file on the SD Card through the Storage Access Framework.
此StackOverflow答案详细说明了如何使用新API和现有限制:
This StackOverflow answer explains in detail how to use the new APIs and the existing limitations:
如何使用为Android 5.0(Lollipop)提供的新SD卡访问API?
这篇关于写入SD卡总是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!