Reputation: 31
Oh Dear, Please help before I have no more hair on my head!
Firstly, here is my code:
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");
}
}`
As you can see: I copied a database file into my Assets folder. Now I want to copy it to a virtual sd card so that I can edit the database. I have added the WRITE permissions in my manifest. I read that I need to change something in my [run configuration] setting - but what?
I keep getting Permission Denied and It says my sd card is not mounted.
Please help!
Upvotes: 3
Views: 2000
Reputation: 19310
You can use following methods to determine whether external storage is available or not
/**
* @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;
}
Add following permissions in Android Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Also check whether you have enabled sd-card for emulator:
Go to 'Android Virtual Device Manager'(AVD)
and click on your Emulater then press 'Edit'
button. In 'Hardware'
section, check whether you have added 'SD card support = yes'
Upvotes: 2