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 卡,以便我可以编辑数据库。我在清单中添加了 WRITE 权限。我读到我需要更改我的 [运行配置] 设置中的某些内容 - 但是什么?

我不断收到 Permission Denied,它说我的 sd 卡没有安装。

请帮忙!

4

1 回答 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 Manifest 中添加以下权限

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

还要检查您是否为模拟器启用了 sd-card:

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

于 2012-04-11T13:41:45.297 回答