简体   繁体   中英

Download file from Google Drive with its URI, without Google Drive API in Android

I don't want the client to do any authentication at all, like signing in his email or anything. The client must just get all the shared files on click.

It has been given at many places, that it can be done by HTTP GET. I have tried it with different type files and sizes in Google Drive, but my code is giving me around 50 kb file, which of course is unreadable.

As an example here:

  1. The source file is a "Poocha.mp3".

  2. I have shareable ID returned by Google for this file.

  3. I have verified that this ID ie shareable string is shareable.

  4. I have taken all permissions in the Android manifest.

     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permission android:name="android.permission.WRITE_CALENDAR" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
  5. I am testing this on a live device not on emulator.

  6. When I copy paste this link " https://drive.google.com/uc?export=download&id=0B5Isk4aTJApuZE9sVzg2MUxvc0U ", the file is downloaded in browser properly.

  7. But my code below is not working for any of the drive link.
    but but but

  8. When I upload the same file on my server and change the URL, the file gets downloaded correctly with the same code below. I have tried the same with "pictures.jpg" files, but every file on Google Drive gives incomplete 50 to 54 kb file and the same code works for the same file on my server.

My code:

protected String doInBackground(String... f_url) {
    int count;
    String id = "0B5Isk4aTJApuZE9sVzg2MUxvc0U";

    try {

        URL url = new URL("https://drive.google.com/uc?export=download&id=" + id);

        HttpURLConnection c = (HttpURLConnection) url.openConnection();

        c.setRequestMethod("GET");

        c.connect();

        int lenghtOfFile = c.getContentLength();

        InputStream input = new BufferedInputStream(url.openStream(), 8192);

        String out = Environment.getExternalStorageDirectory().getAbsolutePath() + "/sifr/album";

        File filedir = new File(out);
        if (!filedir.exists()) {
            filedir.mkdirs();
        }
        OutputStream output = new FileOutputStream(out + "/Poocha.mp3");

        byte data[] = new byte[1024];

        long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress("" + (int) ((total * 100) / lenghtOfFile));
            output.write(data, 0, count);
        }

        output.flush();

        output.close();
        input.close();

    } catch (Exception e) {
        Log.i("GetPathLst errr", e + "");
    }
    return null;
}

Could somebody point out the problem?

I have tried your code with a shared file on my Drive account and it download the file OK. The shared URL I get from Google Drive is of the format:

 https://drive.google.com/open?id=UUjBUTaQyakU0B0HGOaFKlT5geXN 

Other than that, you code worked without modification. (btw, that's not the real id).

I hope this helps.


Update

The above answer, while true, is not your problem. I just tried your URL on my Android device with Google Drive installed and Drive told me that the owner has "trashed" the file. When I tried the same url in my browser, it downloaded OK.

Evidently, the file is available for download but the Drive app seems to make the additional check to make sure the file is not trashed. Browsers don't and can't make this same check.

Move your file from the trash bin or try a different file. It should work.

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