Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1.     <uses-permission android:name="android.permission.INTERNET" />
  2.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  3.     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  4.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  5.  
  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState)
  8.     {
  9.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
  10.         {
  11.             requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  12.             requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
  13.             requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
  14.             requestPermissions(new String[]{Manifest.permission.INTERNET}, 1);
  15.         }
  16. }
  17. private void initWebview()
  18.     {
  19.         myView = (WebView) this.findViewById(R.id.myWebview);
  20.         myView.getSettings().setAllowFileAccess(true);
  21.         myView.getSettings().setJavaScriptEnabled(true);
  22.         myView.setWebViewClient(new WebViewClient());
  23.         myView.setWebChromeClient(new WebChromeClient()
  24.         {
  25.             // For Android 5.0
  26.             public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams)
  27.             {
  28.                 // Double check that we don't have any existing callbacks
  29.                 if (mFilePathCallback != null) {
  30.                     mFilePathCallback.onReceiveValue(null);
  31.                 }
  32.                 mFilePathCallback = filePath;
  33.  
  34.                 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  35.                 if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  36.                     // Create the File where the photo should go
  37.                     File photoFile = null;
  38.                     try {
  39.                         photoFile = createImageFile();
  40.                         takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
  41.                     } catch (IOException ex) {
  42.                         // Error occurred while creating the File
  43.                         Log.e(TAG, "Unable to create Image File", ex);
  44.                     }
  45.  
  46.                     // Continue only if the File was successfully created
  47.                     if (photoFile != null)
  48.                     {
  49.                         mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
  50.                         takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  51.                     }
  52.                     else
  53.                     {
  54.                         takePictureIntent = null;
  55.                     }
  56.                 }
  57.  
  58.                 //File picker
  59.                 Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
  60.                 contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
  61.                 contentSelectionIntent.setType("image/*");
  62.  
  63.                 Intent[] intentArray;
  64.                 if (takePictureIntent != null) {
  65.                     intentArray = new Intent[]{takePictureIntent};
  66.                 } else {
  67.                     intentArray = new Intent[0];
  68.                 }
  69.  
  70.                 Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
  71.                 chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
  72.                 chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
  73.                 chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  74.                 takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
  75.                 takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  76.                 startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
  77.  
  78.                 return true;
  79.             }
  80.  
  81.   private File createImageFile() throws IOException {
  82.         // Create an image file name
  83.         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  84.         String imageFileName = "JPEG_" + timeStamp + "_";
  85.         File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  86.         File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
  87.  
  88.         return imageFile;
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement