android - 如何在android中的外部存储卡上写入
问题描述
我在android中有两个不同的内存位置。一种是内部存储位置,称为外部 sd 卡,另一种是可移动存储卡。我在清单中获得了读写权限。我也在检查它的可用性。我还添加了 android:requestLegacyExternalStorage="true" 我的问题是我无法写入这个可移动媒体。当我尝试写作时,我得到了这个错误。
2020-06-06 20:54:13.334 29148-29148/com.test.debug I/OMD::SDCard: /storage/8E14-3919 is removable external storage
2020-06-06 20:54:13.332 29148-29148/com.test.debug W/sdatabase.debug: type=1400 audit(0.0:8919357): avc: granted { read open } for pid=29148 path="/mnt/media_rw/8E14-3919" dev="mmcblk1p1" ino=1 scontext=u:r:untrusted_app:s0:c74,c258,c512,c768 tcontext=u:object_r:vfat:s0 tclass=dir
2020-06-06 20:54:13.336 29148-29148/com.test.debug W/System.err: java.io.FileNotFoundException: /storage/8E14-3919/freebookslibrary.db: open failed: EACCES (Permission denied)
2020-06-06 20:54:13.336 29148-29148/com.test.debug W/System.err: at libcore.io.IoBridge.open(IoBridge.java:496)
2020-06-06 20:54:13.336 29148-29148/com.test.debug W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
2020-06-06 20:54:13.336 29148-29148/com.test.debug W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:125)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at com.khan.mybookslibrary.BackUpExternal.exportDatabase(BackUpExternal.java:145)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at com.khan.mybookslibrary.BackUpExternal.access$100(BackUpExternal.java:57)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at com.khan.mybookslibrary.BackUpExternal$4.onClick(BackUpExternal.java:115)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:177)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at android.os.Handler.dispatchMessage(Handler.java:107)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at android.os.Looper.loop(Looper.java:213)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at android.app.ActivityThread.main(ActivityThread.java:8147)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
2020-06-06 20:54:13.337 29148-29148/com.test.debug W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err: at libcore.io.Linux.open(Native Method)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err: at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err: at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8015)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err: at libcore.io.IoBridge.open(IoBridge.java:482)
2020-06-06 20:54:13.338 29148-29148/com.test.debug W/System.err: ... 12 more
2020-06-06 20:54:13.406 29148-29212/com.test.debug W/libEGL: EGLNativeWindowType 0x6f62540b90 disconnect failed
> 我试图实现的代码如下:
private void exportDatabase(String fileName) {
final File dbFile = context.getDatabasePath(DATABASE_NAME);//existing database file path
SDCard sdc = new SDCard();
File file = sdc.findSdCardPath(context);
FileChannel source = null;
FileChannel destination = null;
if(file!= null){
String backUpPath = file.getAbsolutePath() + "/" + fileName + ".db";
try {
source = new FileInputStream(dbFile).getChannel();
destination = new FileOutputStream(backUpPath).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
alert(context.getResources().getString(R.string.Database_exported_external));
} catch (IOException e) {
e.printStackTrace();
alert(context.getResources().getString(R.string.Failed_export_database_external));
}
}
else
return;
}
我需要解决这个问题
解决方案
推荐阅读
- wordpress - 如何在 WooCommerce 中更改新标签徽章?
- c - 如果子进程在c(unix)中暂停\睡眠,如何(以编程方式)查找
- python - 为什么 Selenium 中的 HTML 源代码看起来与 Web 浏览器视图中显示的不同?
- javascript - 为分组条形图设置域时出错
- django - Django - 如何按用户过滤购买的书
- angular - ngRx 不会更改状态数据
- php - 分解数组,使每个第二个元素都在不同的数组中
- python - Numpy数组:函数也会影响原始输入对象
- svg - 如何删除两个 SVG 符号连接的边框
- jupyter-notebook - AttributeError:模块“ast”在 Jupyter Notebook 中没有属性“AnnAssign”