如何在android中写外部存储卡

问题描述 投票:0回答:1

我在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;
}

我需要解决这个问题

android
1个回答
0
投票
默认情况下,

定位到[[Android Q-API 29]的应用程序进入外部存储的过滤视图。快速解决方案是在AndroidManifest.xml中添加以下代码:

<manifest ... > <!-- This attribute is "false" by default on apps targeting Android Q. --> <application android:requestLegacyExternalStorage="true" ... > ... </application> </manifest>
在此了解更多信息:https://developer.android.com/training/data-storage/compatibility    
© www.soinside.com 2019 - 2024. All rights reserved.