【问题标题】:How to upload files from Google Drive to my app?如何将文件从 Google Drive 上传到我的应用程序?
【发布时间】:2014-09-05 08:56:38
【问题描述】:

您好,在我的应用程序中,我需要通过 Intent 类将我的数据库文件备份到 Google Drive 帐户,我只需创建文件并将其发送到“Intent.ACTION_SEND”,用户需要选择 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"));
}

【问题讨论】:

    标签: android android-intent google-drive-api


    【解决方案1】:

    您可以使用 Drive Android API 执行此操作。见creating filesworking with file contents.

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      相关资源
      最近更新 更多