Created
February 13, 2019 21:02
-
-
Save asaladino/6d13cd788f0d7eb895bfea15b683c514 to your computer and use it in GitHub Desktop.
Android WebView File Upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:id="@+id/drawer_layout" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" > | |
| <LinearLayout | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical" > | |
| <include layout="@layout/toolbar" /> | |
| <WebView | |
| android:id="@+id/my_webview" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" /> | |
| </LinearLayout> | |
| <include layout="@layout/drawer_list" /> | |
| </android.support.v4.widget.DrawerLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| package="com.codingsimply.project"> | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
| <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
| </manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.codingsimply.project.activities; | |
| import android.annotation.SuppressLint; | |
| import android.content.ActivityNotFoundException; | |
| import android.content.Intent; | |
| import android.net.Uri; | |
| import android.os.Build; | |
| import android.os.Bundle; | |
| import android.support.annotation.RequiresApi; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.webkit.ValueCallback; | |
| import android.webkit.WebChromeClient; | |
| import android.webkit.WebView; | |
| import android.widget.Toast; | |
| import com.codingsimply.project.R; | |
| public class WebUploadActivity extends AppCompatActivity { | |
| @SuppressWarnings("FieldCanBeLocal") | |
| private WebView webView; | |
| public static final int REQUEST_SELECT_FILE = 100; | |
| public ValueCallback<Uri[]> uploadMessage; | |
| @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| loadViewElements(); | |
| loadWebView(); | |
| } | |
| /** | |
| * Load web view and module title. | |
| */ | |
| private void loadViewElements() { | |
| setContentView(R.layout.activity_web); | |
| } | |
| /** | |
| * Build the web view | |
| */ | |
| @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
| @SuppressLint("SetJavaScriptEnabled") | |
| private void loadWebView() { | |
| webView = findViewById(R.id.my_webview); | |
| webView.setWebChromeClient(new MyWebChromeClient(this)); | |
| webView.getSettings().setJavaScriptEnabled(true); | |
| webView.getSettings().setAllowContentAccess(true); | |
| webView.loadUrl(getIntent().getStringExtra("url")); | |
| } | |
| @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
| protected void onActivityResult(int requestCode, int resultCode, Intent data){ | |
| if (requestCode == REQUEST_SELECT_FILE) { | |
| if (uploadMessage == null) return; | |
| uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); | |
| uploadMessage = null; | |
| } | |
| } | |
| class MyWebChromeClient extends WebChromeClient { | |
| private WebUploadActivity myActivity; | |
| MyWebChromeClient(WebUploadActivity activity) { | |
| myActivity = activity; | |
| } | |
| @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | |
| public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { | |
| // make sure there is no existing message | |
| if (myActivity.uploadMessage != null) { | |
| myActivity.uploadMessage.onReceiveValue(null); | |
| myActivity.uploadMessage = null; | |
| } | |
| myActivity.uploadMessage = filePathCallback; | |
| Intent intent = fileChooserParams.createIntent(); | |
| try { | |
| myActivity.startActivityForResult(intent, NeighborsActivity.REQUEST_SELECT_FILE); | |
| } catch (ActivityNotFoundException e) { | |
| myActivity.uploadMessage = null; | |
| Toast.makeText(myActivity, "Cannot open file chooser", Toast.LENGTH_LONG).show(); | |
| return false; | |
| } | |
| return true; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment