2015-02-02 4 views
0

의도를 사용하여 .db 파일을 Google 드라이브로 내보내고 있습니다. .db 파일을 장치의 로컬 폴더를 통해 가져옵니다.Google 드라이브에서 파일을 내보내고 가져 오는 방법은 무엇입니까?

  1. 드라이브에서 파일을 내 장치로 다시 가져올 수 있습니까?
  2. .db 파일을 "백업"하고 파일을 가져 오는 가장 좋은 방법은 무엇입니까?
  3. "google drive api"를 사용하지 않고이 작업을 수행 할 수 있습니까?

도와주세요!

public class BackUpDb { 

     Context context; 

     public BackUpDb(Context activity) { 
      this.context = activity; 
      } 

     public void exportDb(){ 
      File direct = new File(Environment 
        .getExternalStorageDirectory() 
        + "/folderToBeCreated"); 
      if (!direct.exists()) { 
       if (direct.mkdir()) { 
        // directory is created; 
       } 
      } 
      try { 
       exportDB(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 


    public void importDb(){ 
     importDB(); 
    } 



    // importing database 
    @SuppressLint("SdCardPath") 
    private void importDB() { 
     try { 
    //  File file = getBaseContext().getFileStreamPath(Environment.getExternalStorageDirectory() 
    //    + "/MyDatabase"); 
    //  if(file.exists()){ 

       FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory() 
         + "/folderToBeCreated/MyDatabase");  

       String outFileName = "/data/data/com.example.application/databases/"+DbHandler.DB_NAME; 

       OutputStream output = new FileOutputStream(outFileName); 
       byte[] buffer = new byte[1024]; 
       int length; 
       while ((length = fis.read(buffer)) > 0) { 
        output.write(buffer, 0, length); 
       } 
       // Close the streams 
       output.flush(); 
       output.close(); 
       fis.close(); 
       Toast.makeText(context, "Ok :)",Toast.LENGTH_LONG).show(); 




     } catch (Exception e) { 
      Toast.makeText(context, "No list found", 
        Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
    } 


    @SuppressLint("SdCardPath") 
    private void exportDB() throws IOException { 
     // Open your local db as the input stream 
     try { 
      String inFileName = "/data/data/com.example.application/databases/"+DbHandler.DB_NAME; 
      File dbFile = new File(inFileName); 
      FileInputStream fis = new FileInputStream(dbFile); 

      String outFileName = Environment.getExternalStorageDirectory()+ "/folderToBeCreated/MyDatabase"; 


      // Open the empty db as the output stream 
      OutputStream output = new FileOutputStream(outFileName); 
      // transfer bytes from the inputfile to the outputfile 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = fis.read(buffer)) > 0) { 
       output.write(buffer, 0, length); 
      } 
      // Close the streams 
      output.flush(); 
      output.close(); 
      fis.close(); 







     } catch (Exception e) { 
      Toast.makeText(context, e.toString(), Toast.LENGTH_LONG) 
        .show(); 
     } 
     Toast.makeText(context, 
       "save to :\n/folderToBeCreated",Toast.LENGTH_LONG).show(); 


    } 


    public void sendDb(String mailAddres){ 

     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("application/octet-stream"); 
     intent.putExtra(Intent.EXTRA_EMAIL, new String[] {mailAddres}); 
     intent.putExtra(Intent.EXTRA_SUBJECT, "MyDatabase"); 

     File root = Environment.getExternalStorageDirectory(); 
     File file = new File(root, "/folderToBeCreated/MyDatabase"); 
     if (!file.exists() || !file.canRead()) { 
      Toast.makeText(context, "No sd-card", Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     Uri uri = Uri.parse("file://" + file.getAbsolutePath()); 
     intent.putExtra(Intent.EXTRA_STREAM, uri); 
     context.startActivity(Intent.createChooser(intent, "BACKUP")); 
    } 



    } 
+0

좋아요, 안나, 기기에 저장하고 클라우드에 업로드 하시겠습니까? 귀하의 요구 사항을 요약 한 것입니까? – Elltz

+0

예. 제발 .. 도와 주실 수 있습니까? – Anna

답변

-1

Android 4.4 (KitKat)는 외부 SD 카드에 파일을 만들 수 없으므로 제거되었습니다.

+0

나는 그것을 몰랐다. 해킹 할 방법이 있는가? – Anna

+0

아니. 그건 의도적으로 그렇게 했어. – ucsunil

+0

왜? 오케이 .. 나는 내 질문을 변경했다. – Anna