Reputation: 115
I have an app that backs up data to the sd card. When the SD card is inserted, it works perfectly fine and will backup the data. When the SD card is not present, I programed it to create an alert that tells the user that the SD card wasnt found.
The problem I'm having is that if somebody tries to export data for the first time without an SD card present, it will force close. However, if they backup a set of data first and then have no SD card later when they try to backup more data, an alert message appears just how I want.
Somebody help please! Heres my code (modified from a tutorial):
InputStream myInput;
try {
myInput = new FileInputStream("/data/data/com.android.footprint/databases/MY_DATABASE");
File directory = new File("/sdcard/Footprint");
if (!directory.exists())
{
directory.mkdirs();
}
OutputStream myOutput = new FileOutputStream(directory.getPath()+ "/MY_DATABASE.backup");
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
alertDialog2 = new AlertDialog.Builder(
settings.this).create();
alertDialog2.setTitle("Export Complete");
alertDialog2.setMessage("Data Backup successful");
alertDialog2.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog2.dismiss();
}
});
alertDialog2.show();
} catch (FileNotFoundException e) {
alertDialog2.dismiss();
alertDialog3 = new AlertDialog.Builder(
settings.this).create();
alertDialog3.setIcon(R.drawable.delete);
alertDialog3.setTitle("Export Failed");
alertDialog3.setMessage("Make sure SD card is inserted and unmounted");
alertDialog3.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog3.dismiss();
}
});
alertDialog3.show();
} catch (IOException e) {
alertDialog2.dismiss();
alertDialog3 = new AlertDialog.Builder(
settings.this).create();
alertDialog3.setIcon(R.drawable.delete);
alertDialog3.setTitle("Export Failed");
alertDialog3.setMessage("Make sure SD card is inserted and unmounted");
alertDialog3.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog3.dismiss();
}
});
alertDialog3.show();
} catch (Exception e) {
alertDialog2.dismiss();
alertDialog3 = new AlertDialog.Builder(
settings.this).create();
alertDialog3.setIcon(R.drawable.delete);
alertDialog3.setTitle("Export Failed");
alertDialog3.setMessage("Make sure SD card is inserted and unmounted");
alertDialog3.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog3.dismiss();
}
});
alertDialog3.show();
}
Upvotes: 1
Views: 433
Reputation: 4812
Not sure if that is your problem but it would be a good idea to review the way you're checking if the SD Card is present. Here's how I handled it in one of my most recent application:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Everything is fine, write away !
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Card is in read mode, cannot write
Log.e("FileManager", "SD Card is in READ ONLY mode, impossible to write files.");
} else {
// Impossible to access SD Card
Log.e("FileManager", "Having trouble with SD card, can't write nor read from it.");
}
Also, may I suggest you get your path from the static method found in the Environement
Class like so:
File root = Environment.getExternalStorageDirectory();
File directory = new File(root + "/Footprint");
Hope this helps you solve your problem!
Upvotes: 2