gpt4 book ai didi

android - SD 卡和配置设置

转载 作者:太空狗 更新时间:2023-10-29 13:36:19 24 4
gpt4 key购买 nike

哦,亲爱的,在我头上没有更多头发之前,请帮帮我!

首先,这是我的代码:

private void copyDatabase() {
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
Toast.makeText(this, "External SD card not mounted", Toast.LENGTH_LONG).show();
}
//Toast Message Always appears
try {
InputStream in = getAssets().open("Asset_Directory");


File outFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Asset_Directory");
outFile.createNewFile();
OutputStream out = new FileOutputStream(outFile);


byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

out.close();
in.close();
Log.i("AssetCaptureActivity","Database has been transferred");
} catch (IOException e) {
Log.i("AssetCaptureActivity","Could not open the file");
}
}`

如您所见:我将一个数据库文件复制到我的 Assets 文件夹中。现在我想将它复制到虚拟 sd 卡,以便我可以编辑数据库。我在 list 中添加了 WRITE 权限。我读到我需要更改 [运行配置] 设置中的某些内容 - 但是什么?

我一直收到 Permission Denied 消息,它说我的 SD 卡没有安装。

请帮忙!

最佳答案

您可以使用以下方法判断外部存储是否可用

/**
* @return <ul>
* <li><b>true: </b>If external storage is available</li>
* <li><b>false: </b>If external storage is not available</li>
* </ul>
*/
public static boolean isExternalStorageAvailable() {

boolean isAvailable = false;
try {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)
|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can read the media
isAvailable = true;
} else {
// Something else is wrong. It may be one of many other states,
// but all we need
// to know is we can not read
isAvailable = false;
}
} catch (Exception e) {
e.printStackTrace();
}

return isAvailable;
}

/**
* @return <ul>
* <li><b>true: </b>If external storage is writable</li>
* <li><b>false: </b>If external storage is not writable</li>
* </ul>
*/
public static boolean isExternalStorageWritable() {

boolean isWriteable = false;
try {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can write the media
isWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media but we can't write
isWriteable = false;
} else {
// Something else is wrong. It may be one of many other
// states, but all we need
// to know is we can neither read nor write
isWriteable = false;
}
} catch (Exception e) {
e.printStackTrace();
}

return isWriteable;
}

在Android Manifest中添加以下权限

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

同时检查您是否为模拟器启用了 sd 卡:

转到 'Android Virtual Device Manager'(AVD) 并单击您的模拟器,然后按 'Edit' 按钮。在'Hardware'部分,检查是否添加了'SD card support = yes'

关于android - SD 卡和配置设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10106877/

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