Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
- {
- requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
- requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
- requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
- requestPermissions(new String[]{Manifest.permission.INTERNET}, 1);
- }
- }
- private void initWebview()
- {
- myView = (WebView) this.findViewById(R.id.myWebview);
- myView.getSettings().setAllowFileAccess(true);
- myView.getSettings().setJavaScriptEnabled(true);
- myView.setWebViewClient(new WebViewClient());
- myView.setWebChromeClient(new WebChromeClient()
- {
- // For Android 5.0
- public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams)
- {
- // Double check that we don't have any existing callbacks
- if (mFilePathCallback != null) {
- mFilePathCallback.onReceiveValue(null);
- }
- mFilePathCallback = filePath;
- Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
- // Create the File where the photo should go
- File photoFile = null;
- try {
- photoFile = createImageFile();
- takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
- } catch (IOException ex) {
- // Error occurred while creating the File
- Log.e(TAG, "Unable to create Image File", ex);
- }
- // Continue only if the File was successfully created
- if (photoFile != null)
- {
- mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
- takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
- }
- else
- {
- takePictureIntent = null;
- }
- }
- //File picker
- Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
- contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
- contentSelectionIntent.setType("image/*");
- Intent[] intentArray;
- if (takePictureIntent != null) {
- intentArray = new Intent[]{takePictureIntent};
- } else {
- intentArray = new Intent[0];
- }
- Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
- chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
- chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
- chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
- takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
- takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
- startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
- return true;
- }
- private File createImageFile() throws IOException {
- // Create an image file name
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
- String imageFileName = "JPEG_" + timeStamp + "_";
- File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
- File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
- return imageFile;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement