写入SD卡时强行关闭(android)

时间:2012-02-21 20:04:02

标签: android sd-card

我有一个应用程序可以将数据备份到SD卡。插入SD卡后,它可以正常工作并备份数据。当SD卡不存在时,我编写了它以创建一个警报,告诉用户没有找到SD卡。

我遇到的问题是,如果有人试图在没有SD卡的情况下首次导出数据,它将强行关闭。但是,如果他们先备份一组数据,然后在尝试备份更多数据时没有SD卡,则会出现一条警告消息。

请有人帮忙!继承我的代码(从教程中修改):

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();
}

2 个答案:

答案 0 :(得分:2)

使用此代码检查SD卡:

public static boolean isSdPresent() {
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

我在link上找到了这个。希望这会有所帮助..

答案 1 :(得分:2)

不确定这是不是您的问题,但最好还是检查您是否存在SD卡的方式。以下是我在最近的一个应用程序中处理它的方法:

    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.");
    }

此外,我是否可以建议您从Environement类中的静态方法获取路径,如下所示:

    File root = Environment.getExternalStorageDirectory();
    File directory = new File(root + "/Footprint");

希望这可以帮助您解决问题!