Open In App

How to Access Content of Google Drive in Android?

Last Updated : 22 Mar, 2021
Like Article

In certain cases, while uploading files we may require to upload files from our google drive or it may happen that we want to give certain folder access of our google drive to users of our case. Like in the case of providing courses or study material that we have stored in our google drive. Here we going to implement how to give access to our google drive content to our users using Android studio. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add this into the AndroidManifest.xml file

<uses-permission android:name=”android.permission.INTERNET”/>

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
        android:layout_height="match_parent" />

Step 4: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.content.DialogInterface;
import android.os.Bundle;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        // initialising the web view
        WebView wv = (WebView) findViewById(;
        // add your link here
        wv.loadUrl("https://Enter your drive link here");
        wv.setWebViewClient(new Client());
        WebSettings ws = wv.getSettings();
        // Enabling javascript
        // download manager is a service that can be used to handle downloads
        wv.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String s1, String s2, String s3, long l) {
                DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url));
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                Toast.makeText(MainActivity.this, "Downloading....", Toast.LENGTH_SHORT).show();
    private class Client extends WebViewClient {
        // on page started load start loading the url
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        // load the url of our drive
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
            // if stop loading
            try {
            } catch (Exception e) {
            if (webView.canGoBack()) {
            // if loaded blank then show error
            // to check internet connection using
            // alert dialog
            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
            alertDialog.setMessage("Check your internet connection and Try again.");
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
            super.onReceivedError(webView, errorCode, description, failingUrl);


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads