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
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "horizontal" tools:context = ".MainActivity" > < WebView android:id = "@+id/webview" android:layout_width = "match_parent" android:layout_height = "match_parent" /> </ LinearLayout > |
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.app.AlertDialog; import android.app.DownloadManager; import android.content.DialogInterface; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.webkit.DownloadListener; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initialising the web view WebView wv = (WebView) findViewById(R.id.webview); // add your link here wv.setWebViewClient( new Client()); WebSettings ws = wv.getSettings(); // Enabling javascript ws.setJavaScriptEnabled( true ); wv.getSettings().setJavaScriptCanOpenWindowsAutomatically( true ); wv.clearCache( true ); wv.clearHistory(); // download manager is a service that can be used to handle downloads wv.setDownloadListener( new DownloadListener() { @Override public void onDownloadStart(String url, String s1, String s2, String s3, long l) { DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url)); req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); dm.enqueue(req); Toast.makeText(MainActivity. this , "Downloading...." , Toast.LENGTH_SHORT).show(); } }); } private class Client extends WebViewClient { // on page started load start loading the url @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super .onPageStarted(view, url, favicon); } // load the url of our drive @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true ; } @Override 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 { webView.stopLoading(); } catch (Exception e) { } if (webView.canGoBack()) { webView.goBack(); } // if loaded blank then show error // to check internet connection using // alert dialog webView.loadUrl( "about:blank" ); AlertDialog alertDialog = new AlertDialog.Builder(MainActivity. this ).create(); alertDialog.setTitle( "Error" ); 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) { finish(); startActivity(getIntent()); } }); alertDialog.show(); super .onReceivedError(webView, errorCode, description, failingUrl); } } } |
Output: