哦亲爱的,在我头上没有头发之前请帮忙!
首先,这是我的代码:
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,它说我的sd卡没有安装。
请帮忙!
答案 0 :(得分: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卡:
转到'Android Virtual Device Manager'(AVD)
,然后点击您的Emulater,然后按'Edit'
按钮。在'Hardware'
部分,检查您是否添加了'SD card support = yes'