2012-04-11 102 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