简体   繁体   中英

How to upload files from Google Drive to my app?

Hello in my application i need to backUp my Database file to google Drive account via the Intent class, i just create the file and send it on "Intent.ACTION_SEND" and the user need to choose Google drive . now, i want to import it from the google drive to my app again.that is possible? and one more thing.. there is a way to upload it directly to google drive ? because the intent chooser give the user all the options.

here is my code how i save it:

@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"));
}

You can do this using the Drive Android API. See creating files and working with file contents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM