gpt4 book ai didi

java - 权限被拒绝使用 USB 笔式驱动器写入

转载 作者:行者123 更新时间:2023-11-29 08:52:04 25 4
gpt4 key购买 nike

这是我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abc2"
android:versionCode="1"
android:versionName="1.1.8" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我正在尝试将应用程序的数据库复制到 USB pendrive,上次使用的是 SD 卡,它工作正常,但是在我将 SD 卡路径更改为 /mnt/sda/sda2 之后这是错误权限被拒绝,/mnt/sda/sda2 是 USB pendrive 路径

这是将数据库复制到pendrive的复制文件功能

private void copyFile(String inputPath, String inputFile, String outputPath) {

InputStream in = null;
OutputStream out = null;
try {

//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}


in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);

byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;

// write the output file (You have now copied the file)
out.flush();
out.close();
out = null;
Toast.makeText(UltilityActivity.this, "Export successful!", Toast.LENGTH_SHORT).show();

} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
Toast.makeText(UltilityActivity.this, "Export failed", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Log.e("tag", e.getMessage());
Toast.makeText(UltilityActivity.this, "Export failed", Toast.LENGTH_SHORT).show();
}

}
}

这就是我调用复制文件函数的方式

copyFile("/data/data/com.example.abc2/databases/","DB_BusData","/mnt/sda/sda2/");

最后一次复制到SD卡,用这个代码

copyFile("/data/data/com.example.abc2/databases/","DB_BusData","/mnt/sdcard/");

复制到 SD 卡的工作正常,但 USB pendrive 错误:-

03-10 10:58:13.204: D/MainActivity(1832): open failed: EACCES (Permission denied)
03-10 10:58:13.204: D/MainActivity(1832): java.io.IOException: open failed: EACCES (Permission denied)

需要挂载U盘吗?我可以以编程方式安装吗?如何?请给我一个例子?

最佳答案

在Android操作系统中,通常USB pendrive会被称为SD卡,
但是,Androidmanifest.xml 中的权限需要声明不同的权限,USB 外部存储需要媒体启用权限,如下所示:-

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

关于java - 权限被拒绝使用 USB 笔式驱动器写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22298430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com