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 カードがマウントされていないと表示されます。

助けてください!

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 マニフェストに次のアクセス許可を追加します

<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'

于 2012-04-11T13:41:45.297 に答える