SD卡和配置设置

3

噢亲爱的,请在我头发掉光之前帮帮我!

首先,这是我的代码:

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 卡中,以便我可以编辑这个数据库。 我在清单中添加了写入权限。 我读到需要在我的 [运行配置] 设置中更改一些内容——但是具体该怎么做呢?
我不断地收到“权限被拒绝”的提示,并且说我的 SD 卡未挂载。
请帮忙!

你是在模拟器上还是真机上测试? - Hardik4560
是的,我创建了一个 AVD 中的 SD 卡。 - MichelleCreated
1
喜欢你请求帮助的方式(在我头发掉光之前寻求帮助!),但是无法帮助你。 - Zar E Ahmer
1个回答

2
您可以使用以下方法来确定外部存储是否可用:
/**
 * @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清单文件中添加以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

同时,请检查您是否已经为模拟器启用了SD卡:

进入“Android虚拟设备管理器”(AVD),点击您的模拟器,然后按下“编辑”按钮。在“硬件”部分,检查您是否已经添加了“SD卡支持 = 是”


谢谢,我会尝试并回报的! - MichelleCreated
两者都返回false?我该怎么做才能使它们都返回true?如何在模拟器中挂载SD卡? - MichelleCreated
我有 =,(它仍然返回false) - MichelleCreated
进入“Android虚拟设备管理器(AVD)”,点击您的模拟器,然后按“Edit”按钮。在“硬件”部分,检查是否已添加了“SD卡支持=是”。 - Muhammad Nabeel Arif
就是这样!谢谢穆罕默德,你真是个明星! - MichelleCreated

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接