upload image from camera in webview does not work

后端 未结 1 1483
生来不讨喜
生来不讨喜 2021-01-27 06:13

I\'ve been trying to do an upload of images in Workplace from facebook through webview from gallery and from camera.

From gallery it works fine but from camera the image

相关标签:
1条回答
  • 2021-01-27 07:10

    check this it's worked for me.

    public class MainActivity extends Activity {
    
        private WebView webView;
        private ValueCallback<Uri> mUploadMessage;
        private ValueCallback<Uri[]> mUploadMessages;
        private Uri mCapturedImageURI = null;
        private static final int MY_CAMERA_REQUEST_CODE = 100;
        private static final int FILECHOOSER_RESULTCODE = 1;
    
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            webView = findViewById(R.id.webView);
    
            webView.setWebViewClient(new WebViewClient());
    
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setDomStorageEnabled(true);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setDomStorageEnabled(true);
            webView.getSettings().setPluginState(WebSettings.PluginState.ON);
            webView.getSettings().setAppCacheEnabled(false);
            webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setBuiltInZoomControls(false);
            webView.getSettings().setSupportZoom(false);
            webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
            webView.getSettings().setDomStorageEnabled(true);
            webView.getSettings().setDatabaseEnabled(true);
            webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
    
            webView.loadUrl("url");
    
            webView.setWebChromeClient(new WebChromeClient() {
    
                // For api level bellow 24
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
                    if (url.startsWith("http")) {
                        // Return false means, web view will handle the link
                        return false;
                    }
    
                    return false;
                }
    
    
                // From api level 24
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    /*Toast.makeText(mcontext, "New Method",Toast.LENGTH_SHORT).show();*/
    
                    // Get the tel: url
                    String url = request.getUrl().toString();
    
                    if (url.startsWith("http")) {
                        // Return false means, web view will handle the link
                        return false;
                    }
    
                    return false;
                }
    
                // openFileChooser for Android 3.0+
    
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                    mUploadMessage = uploadMsg;
                    openImageChooser();
                }
    
                // For Lollipop 5.0+ Devices
    
                public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
                    mUploadMessages = filePathCallback;
                    openImageChooser();
                    return true;
                }
    
                // openFileChooser for Android < 3.0
    
                public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                    openFileChooser(uploadMsg, "");
                }
    
                //openFileChooser for other Android versions
    
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                    openFileChooser(uploadMsg, acceptType);
                }
            });
    
        }
    
        private void openImageChooser() {
            try {
    
                File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FolderName");
    
                if (!imageStorageDir.exists()) {
                    imageStorageDir.mkdirs();
                }
                File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
                mCapturedImageURI = Uri.fromFile(file);
    
                final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
    
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("image/*");
    
                Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent});
    
                startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
    
            if (requestCode == FILECHOOSER_RESULTCODE) {
    
                if (null == this.mUploadMessage && null == this.mUploadMessages) {
                    return;
                }
    
               /* Uri result;
                if (requestCode != RESULT_OK){
                    result = null;
                }else {
                    result = intent == null ? this.mCapturedImageURI : intent.getData();
                }
                this.mUploadMessage.onReceiveValue(result);
                this.mUploadMessage = null;*/
                if (null != mUploadMessage) {
                    handleUploadMessage(requestCode, resultCode, intent);
    
                } else if (mUploadMessages != null) {
                    handleUploadMessages(requestCode, resultCode, intent);
                }
            }
        }
    
        private void handleUploadMessage(int requestCode, int resultCode, Intent intent) {
            Uri result = null;
            try {
                if (resultCode != RESULT_OK) {
                    result = null;
                } else {
                    // retrieve from the private variable if the intent is null
    
                    result = intent == null ? mCapturedImageURI : intent.getData();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        private void handleUploadMessages(int requestCode, int resultCode, Intent intent) {
            Uri[] results = null;
            try {
                if (resultCode != RESULT_OK) {
                    results = null;
                } else {
                    if (intent != null) {
                        String dataString = intent.getDataString();
                        ClipData clipData = intent.getClipData();
                        if (clipData != null) {
                            results = new Uri[clipData.getItemCount()];
                            for (int i = 0; i < clipData.getItemCount(); i++) {
                                ClipData.Item item = clipData.getItemAt(i);
                                results[i] = item.getUri();
                            }
                        }
                        if (dataString != null) {
                            results = new Uri[]{Uri.parse(dataString)};
                        }
                    } else {
                        results = new Uri[]{mCapturedImageURI};
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            mUploadMessages.onReceiveValue(results);
            mUploadMessages = null;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题