You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2019/01/29 16:55:46 UTC

[GitHub] gilbertogwa commented on issue #328: Can not open camera when I click input type="file" on Android devices

gilbertogwa commented on issue #328: Can not open camera when I click input type="file" on Android devices
URL: https://github.com/apache/cordova-plugin-inappbrowser/issues/328#issuecomment-458618536
 
 
   I solve change InAppBrowser plugin. I merged solution on https://stackoverflow.com/questions/51168600/webview-open-camera-from-input-field-without-filechooser.
   
   Follow the changes:
   
   See the changes made in InAppBrowser class from plugin version 3.0.0
   
   1 - include imports:
   
   ```
   import android.app.Activity;
   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;
   
   ```
   2 - declare variavel;
   
   
   ```
       private String mCM;
   
   ```
   
   3 - Replace onShowFileChooser code
   
   ```
   
                       // For Android 5.0+
                       public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
                       {
                           LOG.d(LOG_TAG, "File Chooser 5.0+");
   
                           // If callback exists, finish it.
                           if(mUploadCallbackLollipop != null) {
                               mUploadCallbackLollipop.onReceiveValue(null);
                           }
                           mUploadCallbackLollipop = 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_TITLE, "Selecione a imagem");
                           chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
   
                           // Run cordova startActivityForResult
                           cordova.startActivityForResult(InAppBrowser.this, chooserIntent, FILECHOOSER_REQUESTCODE);
   
                           return true;
                       }
   
   ```
   
   4 - create method
   
   
   ```
   
       private File createImageFile() throws IOException{
           @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
           String imageFileName = "img_"+timeStamp+"_";
           File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
           return File.createTempFile(imageFileName,".jpg",storageDir);
       }
   
   ```
   
   5 - Replace onActivityResult
   
   ```
   
       public void onActivityResult(int requestCode, int resultCode, Intent intent) {
           // For Android >= 5.0
           if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   
               LOG.d(LOG_TAG, "onActivityResult (For Android >= 5.0)");
   
               Uri[] results = null;
               //Check if response is positive
               if(resultCode== Activity.RESULT_OK){
                   if(requestCode == FILECHOOSER_REQUESTCODE){
                       if(null == mUploadCallbackLollipop){
                           return;
                       }
                       if(intent == null || intent.getData() == null){
                           //Capture Photo if no image available
                           if(mCM != null){
                               results = new Uri[]{Uri.parse(mCM)};
                           }
                       }else{
                           String dataString = intent.getDataString();
                           if(dataString != null){
                               results = new Uri[]{Uri.parse(dataString)};
                           }
                       }
                   }
               }
               mUploadCallbackLollipop .onReceiveValue(results);
               mUploadCallbackLollipop = null;
           }
           // For Android < 5.0
           else {
               LOG.d(LOG_TAG, "onActivityResult (For Android < 5.0)");
               // If RequestCode or Callback is Invalid
               if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
                   super.onActivityResult(requestCode, resultCode, intent);
                   return;
               }
   
               if (null == mUploadCallback) return;
               Uri result = intent == null || resultCode != cordova.getActivity().RESULT_OK ? null : intent.getData();
   
               mUploadCallback.onReceiveValue(result);
               mUploadCallback = null;
           }
       }
   
   ```
   
   https://stackoverflow.com/questions/26381050/choose-camera-in-file-upload-in-cordova-application-on-android-without-using-cor/54425770#54425770
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services

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