Home Load Iframe in webView - Android?
Reply: 0

Load Iframe in webView - Android?

user11428
1#
user11428 Published in April 23, 2018, 9:41 am

I have a url that load it in a webView.

Problem : In more device the url load good but on some device certain part of url don't show any. What can I do ?

The content of url contain Iframe.

This problem on android 4.3

Here is my code :

public class WebViewConnectToTeacherFragment extends Fragment implements
        MainActivity.OnBackPressedListenerTabWorkTable {
    @BindView(R.id.wvConnectToTeacher)
    WebView wvConnectToTeacher;
    @BindView(R.id.prBarConnectToTeacher)
    ProgressBar prBarConnectToTeacher;


    private String mCM;
    private ValueCallback<Uri> mUM;
    private ValueCallback<Uri[]> mUMA;
    private final static int FCR = 1;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (Build.VERSION.SDK_INT >= 21) {
            Uri[] results = null;
            //Check if response is positive
            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == FCR) {
                    if (null == mUMA) {
                        return;
                    }
                    if (intent == 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)};
                        }
                    }
                }
            }
            mUMA.onReceiveValue(results);
            mUMA = null;
        } else {
            if (requestCode == FCR) {
                if (null == mUM) return;
                Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData();
                mUM.onReceiveValue(result);
                mUM = null;
            }
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup viewGroup, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_web_view_connect_to_teacher, viewGroup, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);

        if (getActivity() != null) {
            ((MainActivity) getActivity()).setOnBackPressedListenerTabWorkTable(WebViewConnectToTeacherFragment.this);
            Bundle arguments = getArguments();
            if (arguments != null) {
                //region FromWorkTableFragment
                String urlConnectProfessor = arguments.getString("CONNECT_PROFESSOR", null);
                //endregion
                if (urlConnectProfessor != null && !TextUtils.isEmpty(urlConnectProfessor)) {
                    if (isAdded()) {
                        loadWebView(urlConnectProfessor);
                    }
                }
            }
        }
    }

    public class Callback extends WebViewClient {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(getActivity(), "Failed loading app!", Toast.LENGTH_SHORT).show();
        }
    }

    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);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void doBackTabWorkTable(String nameFragment) {
        if (wvConnectToTeacher.canGoBack()) {
            wvConnectToTeacher.goBack();
        } else {
            if (getActivity() != null) {
                if (nameFragment.equals("WebViewConnectToTeacherFragment")) {
                    BusDisplayStatusFrameLayout busDisplayStatusFrameLayout = new BusDisplayStatusFrameLayout();
                    busDisplayStatusFrameLayout.setDisplayStatusFrameLayout(1);
                    EventBus.getDefault().post(busDisplayStatusFrameLayout);
                    android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentManager.popBackStackImmediate();
                    fragmentTransaction.remove(WebViewConnectToTeacherFragment.this);
                    fragmentTransaction.commit();
                }
            }
        }
    }

    //@SuppressLint("SetJavaScriptEnabled")
    private void loadWebView(String url) {

        if (Build.VERSION.SDK_INT >= 23 && (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
        }

        assert wvConnectToTeacher != null;
        WebSettings webSettings = wvConnectToTeacher.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        wvConnectToTeacher.setWebViewClient(new CustomBrowsers());
        wvConnectToTeacher.getSettings().setLoadsImagesAutomatically(true);
        wvConnectToTeacher.getSettings().setJavaScriptEnabled(true);
        wvConnectToTeacher.clearHistory();
        wvConnectToTeacher.setHorizontalScrollBarEnabled(false);
        wvConnectToTeacher.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        wvConnectToTeacher.loadUrl(url);

        if (Build.VERSION.SDK_INT >= 21) {
            webSettings.setMixedContentMode(0);
            wvConnectToTeacher.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else if (Build.VERSION.SDK_INT >= 19) {
            wvConnectToTeacher.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else if (Build.VERSION.SDK_INT < 19) {
            wvConnectToTeacher.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        wvConnectToTeacher.setWebViewClient(new Callback());
        wvConnectToTeacher.setWebChromeClient(new WebChromeClient() {
            //For Android 3.0+
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                mUM = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                WebViewConnectToTeacherFragment.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
            }

            // For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
            public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
                mUM = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                WebViewConnectToTeacherFragment.this.startActivityForResult(
                        Intent.createChooser(i, "File Browser"),
                        FCR);
            }

            //For Android 4.1+
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                mUM = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                WebViewConnectToTeacherFragment.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), WebViewConnectToTeacherFragment.FCR);
            }

            //For Android 5.0+
            public boolean onShowFileChooser(
                    WebView webView, ValueCallback<Uri[]> filePathCallback,
                    WebChromeClient.FileChooserParams fileChooserParams) {
                if (mUMA != null) {
                    mUMA.onReceiveValue(null);
                }
                mUMA = filePathCallback;
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                        takePictureIntent.putExtra("PhotoPath", mCM);
                    } catch (IOException ex) {
                        //Log.e(TAG, "Image file creation failed", ex);
                    }
                    if (photoFile != null) {
                        mCM = "file:" + photoFile.getAbsolutePath();
                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                    } else {
                        takePictureIntent = null;
                    }
                }
                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, "Image Chooser");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
                startActivityForResult(chooserIntent, FCR);
                return true;
            }
        });

        wvConnectToTeacher.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (prBarConnectToTeacher.getVisibility() == View.VISIBLE) {
                    prBarConnectToTeacher.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (prBarConnectToTeacher.getVisibility() == View.GONE) {
                    prBarConnectToTeacher.setVisibility(View.VISIBLE);
                }
            }
        });

        wvConnectToTeacher.loadUrl(url);
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.297951 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO