Created
March 10, 2016 14:53
-
-
Save hndr91/be38c87afa1a943fe934 to your computer and use it in GitHub Desktop.
WebView with upload ability
This file contains 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.webview; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.KeyEvent; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.webkit.ValueCallback; | |
import android.webkit.WebChromeClient; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
public class MainActivity extends AppCompatActivity { | |
//Define URL | |
private static final String URL = "http://192.168.1.101/webview/"; | |
//Define WebView | |
private WebView webView; | |
private ValueCallback<Uri> mUploadMessage; | |
private final static int FILECHOOSER_RESULTCODE=1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
webView = (WebView)findViewById(R.id.webView); //get webView | |
webView.setWebViewClient(new WebViewClient()); //set webView client | |
//set Upload File function | |
webView.setWebChromeClient(new WebChromeClient(){ | |
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { | |
mUploadMessage = uploadMsg; | |
Intent i = new Intent(Intent.ACTION_GET_CONTENT); | |
i.addCategory(Intent.CATEGORY_OPENABLE); | |
//set file type | |
i.setType("image/*"); | |
//launch chooser window | |
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); | |
} | |
}); | |
WebSettings webSettings = webView.getSettings();// initiate webView settings | |
webSettings.setJavaScriptEnabled(true); //allow webView perform javascript | |
webSettings.setBuiltInZoomControls(true); //show zoom control | |
webView.loadUrl(URL); //load URL | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { | |
if (requestCode == FILECHOOSER_RESULTCODE) { | |
if (null == mUploadMessage) | |
return; | |
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); | |
mUploadMessage.onReceiveValue(result); | |
mUploadMessage = null; | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
int id = item.getItemId(); | |
if (id == R.id.action_exit) { | |
//close app when click Close setting menu | |
finishApp(); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
/** | |
* Handle Back Navigation Button | |
* @param keyCode | |
* @param event | |
* @return | |
*/ | |
@Override | |
public boolean onKeyDown(int keyCode, KeyEvent event) { | |
if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) { | |
//back to previous url | |
webView.goBack(); | |
return true; | |
} | |
return super.onKeyDown(keyCode, event); | |
} | |
/** | |
* Method to close App | |
*/ | |
private void finishApp() { | |
finish(); | |
moveTaskToBack(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment