2014-09-05 20 views
0

你好,在我的應用程序中,我需要通過Intent類將數據庫文件備份到Google雲端硬盤帳戶,我只需創建該文件並將其發送到「Intent.ACTION_SEND」,用戶需要選擇Google驅動器。現在,我想將它從谷歌驅動器導入到我的應用程序again.that是可能的? 還有一件事..有一種方法可以直接上傳到谷歌驅動器?因爲意圖選擇器爲用戶提供了所有選項。如何將文件從Google Drive上傳到我的應用程序?

這裏是我的代碼,我如何保存:

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

     String outFileName = Environment.getExternalStorageDirectory() + "/work/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, 
      "Uploaded.", 
      Toast.LENGTH_LONG).show(); 
} 


public void sendDb(String mailAddres){ 

    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("text/plain"); 
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] {mailAddres}); 
    intent.putExtra(Intent.EXTRA_SUBJECT, "my file"); 

    File root = Environment.getExternalStorageDirectory(); 
    File file = new File(root, "/work/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, "Send")); 
} 

回答

相關問題