2016-06-21 4 views
0

내 응용 프로그램에서 간단한 데이터베이스 백업을 실행하려고하는데 장치를 내 PC에 연결할 때 파일이 표시되지 않지만 Android 파일 관리자.내 응용 프로그램에서 복사 한 파일이 PC에 나타나지 않습니다.

: 여기 FileUtil.copyFile 기능이 어떻게 생겼는지

//... 
private void backupDatabase(){ 

    String downloadsPath = Environment 
      .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) 
      .getAbsolutePath(); 
    String backupDirectoryPath = downloadsPath+"/myapp_backups/"; 
    File backupDirectory = new File(backupDirectoryPath); 
    backupDirectory.mkdirs(); 

    String bkpFileName = "backup_"+(new Date().getTime())+".bkp"; 

    String src = mContext.getDatabasePath(DatabaseHelper.DATABASE_NAME).getAbsolutePath(); 
    String dest = backupDirectoryPath + bkpFileName; 
    FileUtil.copyFile(src, dest); 

} 
//... 

그리고 :이 파일은 다음

내가 그것을 복사하고 있는데 코드입니다 ... 그런데 "다운로드"폴더에 복사되는
public static boolean copyFile(String src, String dest){ 

    boolean success; 

    try{ 
     if(!isFile(src)){ 
      throw new Exception("Source file doesn't exist: "+src); 
     } 

     InputStream in = new FileInputStream(src); 
     OutputStream out = new FileOutputStream(dest); 

     // Transfer bytes from in to out 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 
     in.close(); 
     out.close(); 

     success = true; 
    }catch (Exception exception){ 
     exception.printStackTrace(); 
     success = false; 
    } 

    return success; 

} 

이 코드는 테스트 한 두 장치에서 작동하지만 아무 것도 PC에 파일을 표시하지 않습니다.

내가 무엇이 누락 되었습니까?

+0

https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so- : 여기

최종 코드 that-are-visibl – CommonsWare

+0

감사합니다 @ CommonSWare, 나는 모양을 가질 것입니다 – CarlosCarucce

+0

장치를 다시 시작하려고 했습니까? – andre3wap

답변

0

@CommonsWare와 연결된 질문에서 지적한 바와 같이 MediaScannerConnection 클래스에 대해 약간의 연구를했으며 문제를 해결했습니다.

String downloadsPath = Environment 
     .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) 
     .getAbsolutePath(); 
String backupDirectoryPath = downloadsPath+"/myapp_backups/"; 
File backupDirectory = new File(backupDirectoryPath); 
backupDirectory.mkdirs(); 

String bkpFileName = "backup_"+(new Date().getTime())+".bkp"; 

String src = mContext.getDatabasePath(DatabaseHelper.DATABASE_NAME).getAbsolutePath(); 
String dest = backupDirectoryPath + bkpFileName; 
FileUtil.copyFile(src, dest); 

//Scan the new file, so it will show up in the PC file explorer: 
MediaScannerConnection.scanFile(
    mContext, new String[]{dest}, null, null 
);