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();
}
use this code for check SD card:
I found this on this link. Hope this helps..