You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by GitBox <gi...@apache.org> on 2021/02/12 08:54:08 UTC

[GitHub] [cordova-plugin-inappbrowser] marcbrouwer commented on issue #328: Can not open camera when I click input type="file" on Android devices

marcbrouwer commented on issue #328:
URL: https://github.com/apache/cordova-plugin-inappbrowser/issues/328#issuecomment-778065079


   @ReneDyhr The above changes can also be applied to 5.0.1-dev. What I used was:
   
   Add:
   ```
   import android.app.Activity;
   import android.Manifest;
   import android.os.Environment;
   import android.provider.MediaStore;
   import android.util.Log;
   import java.io.File;
   import java.io.IOException;
   import java.text.SimpleDateFormat;
   import java.util.Date;
   ```
   
   Add:
   `private String mCM;`
   
   Change onShowFileChooser to:
   ```
   // For Android 5.0+
   public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
   {
       if(Build.VERSION.SDK_INT >=23 && (cordova.getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || cordova.getActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
           cordova.getActivity().requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
       }
   
       LOG.d(LOG_TAG, "File Chooser 5.0+");
   
       // If callback exists, finish it.
       if(mUploadCallback != null) {
           mUploadCallback.onReceiveValue(null);
       }
       mUploadCallback = filePathCallback;
   
       Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   
       if(takePictureIntent.resolveActivity(cordova.getActivity().getPackageManager()) != null) {
   
           File photoFile = null;
           try{
               photoFile = createImageFile();
               takePictureIntent.putExtra("PhotoPath", mCM);
           }catch(IOException ex){
               Log.e(LOG_TAG, "Image file creation failed", ex);
           }
           if(photoFile != null){
               mCM = "file:" + photoFile.getAbsolutePath();
               takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
           }else{
               takePictureIntent = null;
           }
       }
       // Create File Chooser Intent
       Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
       contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
       contentSelectionIntent.setType("*/*");
       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_INITIAL_INTENTS, intentArray);
   
       // Run cordova startActivityForResult
       cordova.startActivityForResult(InAppBrowser.this, chooserIntent, FILECHOOSER_REQUESTCODE);
   
       return true;
   }
   ```
                       
   Note that I did not include Android < 5.0 for onShowFileChooser
   
   Add:
   ```
   private File createImageFile() throws IOException{
       @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
       String imageFileName = "img_"+timeStamp+"_";
       if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
           // let's use the new api for accessing external storage on 29 onwards
           File storageDir = this.cordova.getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
           File file = new File(storageDir, imageFileName + ".jpg");
           return file;
       } else {
           // was working well on older droids so let's leave it as is.
           File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
           File file =  File.createTempFile(imageFileName,".jpg",storageDir);
           return file;
       }
   }
   ```
   
   Credits to the people above


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org