2012-02-21 3 views
1

데이터를 SD 카드에 백업하는 앱이 있습니다. SD 카드가 삽입되면 완벽하게 작동하여 데이터를 백업합니다. SD 카드가 없으면 사용자에게 SD 카드를 찾을 수 없다는 경고를 생성하도록 프로그래밍했습니다.SD 카드에 기록 할 때 강제 종료 (안드로이드)

내가 겪고있는 문제는 누군가 SD 카드가 없어도 처음으로 데이터를 내보내려고하면 강제로 닫힙니다. 그러나 데이터를 먼저 백업 한 다음 나중에 더 많은 데이터를 백업하려고 할 때 SD 카드가없는 경우 경고 메시지가 나타납니다.

누군가 도움주세요! (튜토리얼에서 수정) Heres는 내 코드 :

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

사용 체크 SD 카드 코드 :

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

내가이 link에이를 발견했다. 희망이 .. ..

2

당신의 문제인지는 확실하지 않지만 SD 카드가 있는지 확인하는 방법을 검토하는 것이 좋습니다.

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

희망이 : 또한

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 클래스에있는 정적 메서드에서 경로를 가져 제안 할 수 있습니다 : 내 가장 최근의 응용 프로그램 중 하나를 처리하는 방법은 다음과 귀하의 문제를 해결하는 데 도움이됩니다!

관련 문제