写入 SD 卡时强制关闭 (android)
我有一个可以将数据备份到 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此代码检查 SD 卡:
我在此 链接。希望这有帮助..
use this code for check SD card:
I found this on this link. Hope this helps..
不确定这是否是您的问题,但最好检查一下检查 SD 卡是否存在的方式。以下是我在最近的一个应用程序中处理它的方法:
另外,我可以建议您从
Environment
类中找到的静态方法获取路径,如下所示:希望这可以帮助您解决问题!
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:
Also, may I suggest you get your path from the static method found in the
Environement
Class like so:Hope this helps you solve your problem!