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

发布于 2025-01-07 18:37:11 字数 2781 浏览 3 评论 0原文

我有一个可以将数据备份到 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();
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖正好 2025-01-14 18:37:11

使用此代码检查 SD 卡:

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

我在此 链接。希望这有帮助..

use this code for check SD card:

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

I found this on this link. Hope this helps..

心清如水 2025-01-14 18:37:11

不确定这是否是您的问题,但最好检查一下检查 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.");
    }

另外,我可以建议您从 Environment 类中找到的静态方法获取路径,如下所示:

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

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文