Guest User

Untitled

a guest
Jul 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.webkit.JavascriptInterface;
  7. import android.webkit.ValueCallback;
  8. import android.webkit.WebChromeClient;
  9. import android.webkit.WebView;
  10. import android.webkit.WebViewClient;
  11. import java.io.*;
  12. import android.util.*;
  13. import java.text.*;
  14. import android.annotation.*;
  15. import java.util.*;
  16. import android.app.*;
  17. import android.provider.*;
  18. import android.os.*;
  19. import android.webkit.*;
  20.  
  21. public class MainActivity extends Activity {
  22.  
  23. public static final int REQUEST_CODE_LOLIPOP = 1;
  24. private final static int RESULT_CODE_ICE_CREAM = 2;
  25. private WebView webView;
  26. private ValueCallback<Uri[]> mFilePathCallback;
  27. private String mCameraPhotoPath;
  28. private ValueCallback<Uri> mUploadMessage;
  29.  
  30. private String url ="http://up.example.com";
  31.  
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.main);
  36.  
  37.  
  38. webView = (WebView) findViewById(R.id.wbvw);
  39. setUpWebViewDefaults(webView);
  40. webView.loadUrl(url);
  41.  
  42.  
  43.  
  44. webView.setWebChromeClient(new WebChromeClient() {
  45. private String TAG;
  46. // For Android 3.0+
  47. public void openFileChooser(ValueCallback<Uri> uploadMsg) {
  48. mUploadMessage = uploadMsg;
  49. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  50. i.addCategory(Intent.CATEGORY_OPENABLE);
  51. i.setType("image/*");
  52. startActivityForResult(Intent.createChooser(i, "File Chooser"),
  53. RESULT_CODE_ICE_CREAM);
  54.  
  55. }
  56. // For Android 3.0+
  57. public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
  58. mUploadMessage = uploadMsg;
  59. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  60. i.addCategory(Intent.CATEGORY_OPENABLE);
  61. i.setType("*/*");
  62. startActivityForResult(Intent.createChooser(i, "File Browser"),
  63. RESULT_CODE_ICE_CREAM);
  64. }
  65. //For Android 4.1
  66. public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
  67. mUploadMessage = uploadMsg;
  68. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  69. i.addCategory(Intent.CATEGORY_OPENABLE);
  70. i.setType("image/*");
  71. startActivityForResult(Intent.createChooser(i, "File Chooser"),
  72. RESULT_CODE_ICE_CREAM);
  73.  
  74. }
  75. //For Android5.0+
  76. public boolean onShowFileChooser(
  77. WebView webView, ValueCallback<Uri[]> filePathCallback,
  78. FileChooserParams fileChooserParams) {
  79. if (mFilePathCallback != null) {
  80. mFilePathCallback.onReceiveValue(null);
  81. }
  82. mFilePathCallback = filePathCallback;
  83. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  84. if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
  85. // Create the File where the photo should go
  86. File photoFile = null;
  87. try {
  88. photoFile = createImageFile();
  89. takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
  90. } catch (IOException ex) {
  91. // Error occurred while creating the File
  92. Log.e(TAG, "Unable to create Image File", ex);
  93. }
  94. // Continue only if the File was successfully created
  95. if (photoFile != null) {
  96. mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
  97. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
  98. Uri.fromFile(photoFile));
  99. } else {
  100. takePictureIntent = null;
  101. }
  102. }
  103. Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
  104. contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
  105. contentSelectionIntent.setType("image/*");
  106. Intent[] intentArray;
  107. if (takePictureIntent != null) {
  108. intentArray = new Intent[]{takePictureIntent};
  109. } else {
  110. intentArray = new Intent[0];
  111. }
  112. Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
  113. chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
  114. chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
  115. chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  116.  
  117. startActivityForResult(chooserIntent, REQUEST_CODE_LOLIPOP);
  118.  
  119. return true;
  120. }
  121. });
  122. }
  123. private File createImageFile() throws IOException {
  124. // Create an image file name
  125. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  126. String imageFileName = "JPEG_" + timeStamp + "_";
  127. File storageDir = Environment.getExternalStoragePublicDirectory(
  128. Environment.DIRECTORY_PICTURES);
  129. File imageFile = File.createTempFile(
  130. imageFileName, /* prefix */
  131. ".jpg", /* suffix */
  132. storageDir /* directory */
  133. );
  134. return imageFile;
  135. }
  136. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  137. private void setUpWebViewDefaults(WebView webView) {
  138. WebSettings settings = webView.getSettings();
  139. // Enable Javascript
  140. settings.setJavaScriptEnabled(true);
  141. // Use WideViewport and Zoom out if there is no viewport defined
  142. settings.setUseWideViewPort(true);
  143. settings.setLoadWithOverviewMode(true);
  144. // Enable pinch to zoom without the zoom buttons
  145. settings.setBuiltInZoomControls(true);
  146. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
  147. // Hide the zoom controls for HONEYCOMB+
  148. settings.setDisplayZoomControls(false);
  149. }
  150. // Enable remote debugging via chrome://inspect
  151. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  152. WebView.setWebContentsDebuggingEnabled(true);
  153. }
  154. // We set the WebViewClient to ensure links are consumed by the WebView rather
  155. // than passed to a browser if it can
  156. webView.setWebViewClient(new WebViewClient());
  157. }
  158. @Override
  159. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  160. super.onActivityResult(requestCode, resultCode, data);
  161. switch (requestCode) {
  162. case RESULT_CODE_ICE_CREAM:
  163. Uri uri = null;
  164. if (data != null) {
  165. uri = data.getData();
  166. }
  167. mUploadMessage.onReceiveValue(uri);
  168. mUploadMessage = null;
  169. break;
  170. case REQUEST_CODE_LOLIPOP:
  171. Uri[] results = null;
  172. // Check that the response is a good one
  173. if (resultCode == Activity.RESULT_OK) {
  174. if (data == null) {
  175. // If there is not data, then we may have taken a photo
  176. if (mCameraPhotoPath != null) {
  177. results = new Uri[]{Uri.parse(mCameraPhotoPath)};
  178. }
  179. } else {
  180. String dataString = data.getDataString();
  181. if (dataString != null) {
  182. results = new Uri[]{Uri.parse(dataString)};
  183. }
  184. }
  185. }
  186. mFilePathCallback.onReceiveValue(results);
  187. mFilePathCallback = null;
  188. break;
  189. }
  190. }
Add Comment
Please, Sign In to add comment