2012-04-11 132 views
3

哦親愛的,請在我沒有更多頭髮在我的頭上之前幫忙!SD卡和配置設置

首先,這裏是我的代碼:

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"); 
    } 
}` 

正如你可以看到: 我複製一個數據庫文件到我的資產文件夾中。 現在我想將它複製到虛擬SD卡上,以便我可以編輯數據庫。 我在清單中添加了WRITE權限。 我看到我需要在我的[運行配置]設置中更改某些內容 - 但是什麼?

我不斷收到權限被拒絕,它說我的SD卡沒有安裝。

請幫忙!

+0

您是否在模擬器或設備上嘗試此操作? – Hardik4560 2012-04-11 13:36:58

+0

是的我是,我在我的avd – Michelle1109Andriod 2012-04-11 13:43:57

+1

中創建了一張SD卡,就像您請求的方式(在我頭上沒有更多頭髮之前的幫助!),但無法幫助您 – Nepster 2015-10-05 15:22:51

回答

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)並點擊您的Emulater然後按'Edit'按鈕。在'Hardware'部分,檢查您是否已添加'SD card support = yes'

+0

謝謝,我會試試這個並報告回來! – Michelle1109Andriod 2012-04-11 13:43:29

+0

都返回false?我該怎麼做才能讓他們都回歸真實。我如何在我的模擬器中安裝SD卡。 – Michelle1109Andriod 2012-04-11 13:48:12

+0

你有沒有把讀寫權限設置爲Android清單 – 2012-04-11 13:53:13