在Android系统中怎样用WebView打开一些非HTTP开头的网址

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Android系统中怎样用WebView打开一些非HTTP开头的网址相关的知识,希望对你有一定的参考价值。

像淘宝网的聚划算的商品网址开头都是 jhs:// ,用WebView 点击这种商品时,就提示找不到网页,请问这种问题如何解决?

参考技术A 用客户端吧,API接口对不上

Android WebView 调用系统拍照和相册

参考技术A 由于业务需要,APP内嵌H5,需要调去系统相册和拍照,网上找了点资料,整理一下,供大家参考:

private static final int REQUEST_CAMERA =1;

private static final int REQUEST_CHOOSE =2;

private ValueCallbackmUploadMessage;

private ValueCallbackmUploadMessagesAboveL;

private UricameraUri;

//5.0以后的方法

webView.setWebChromeClient(new WebChromeClient()

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

@Override

    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams)

if (mUploadMessagesAboveL !=null)

mUploadMessagesAboveL.onReceiveValue(null);

            mUploadMessagesAboveL =null;

        else

mUploadMessagesAboveL = filePathCallback;

            selectImage();

       

return true;

   

);

//选择图片和拍照,对应的string文件,可以自己写死:拍照,相册,取消

private void selectImage()

ActionSheet.createBuilder(this, getSupportFragmentManager()).

setOtherButtonTitles(new String[]getResources().getString(R.string.common_tip_photos), getResources().getString(R.string.common_picture)).

setCancelButtonTitle(getResources().getString(R.string.common_cancel)).setCancelableOnTouchOutside(true).setListener(new ActionSheet.ActionSheetListener()

public void onDismiss(ActionSheet actionSheet, boolean isCancel)

if (mUploadMessage !=null)

mUploadMessage.onReceiveValue(null);

                mUploadMessage =null;

           

if (mUploadMessagesAboveL !=null)

mUploadMessagesAboveL.onReceiveValue(null);

                mUploadMessagesAboveL =null;

           

actionSheet.dismiss();

       

@RequiresApi(api = Build.VERSION_CODES.M)

public void onOtherButtonClick(ActionSheet actionSheet, int index)

switch (index)

case 0:

chosePicture();

break;

                case 1:

setRequestCamera();

           



).show();



//拍照,添加权限申请  这个可以自己写下,我这边是项目中写好的,直接拿过来用了

public void setRequestCamera()

permissionsBuilder =new YXTPermissionsBuilder.Builder(this)

.setOnGrantedListener((requestCode, perms) ->

openCamera();

            )

.setRationale4NeverAskAgain(LanguageUtils.isEnglish() ? String.format(getString(R.string.permission_tips), getString(R.string.common_camera), getString(R.string.app_name), getString(R.string.app_name)) :

String.format(getString(R.string.permission_tips), getString(R.string.app_name), getString(R.string.common_camera), getString(R.string.app_name)))

//必需

            .setRequestCode(ConstantsData.GET_CAMERA)

.build();

    permissionsBuilder.requestPermissions(Manifest.permission.CAMERA);



/**

* 本地相册选择图片

*/

private void chosePicture()

Intent innerIntent =new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    innerIntent.setType("image/*");

    Intent wrapperIntent = Intent.createChooser(innerIntent, null);

    startActivityForResult(wrapperIntent, REQUEST_CHOOSE);



/**

* 打开照相机

*/

private void openCamera()

Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    String imagePaths = Environment.getExternalStorageDirectory().getPath() +"/pbccrc/Images/" + (System.currentTimeMillis() +".jpg");

    // 必须确保文件夹路径存在,否则拍照后无法完成回调

    File vFile =new File(imagePaths);

    if (!vFile.exists())

File vDirPath = vFile.getParentFile();

        vDirPath.mkdirs();

    else

if (vFile.exists())

vFile.delete();

       



cameraUri = FileProvider.getUriForFile(

this,

            getPackageName() +".fileprovider",

            vFile);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);

    startActivityForResult(intent, REQUEST_CAMERA);



/**

* 选择照片后结束

*

* @param data

*/

private UriafterChosePic(Intent data)

if (data !=null)

final String path = data.getData().getPath();

        if (path !=null && (path.endsWith(".png") || path.endsWith(".PNG") || path.endsWith(".jpg") || path.endsWith(".JPG")))

return data.getData();

        else

Toast.makeText(this, "上传的图片仅支持png或jpg格式", Toast.LENGTH_SHORT).show();

       



return null;



/**

* 返回文件选择

*/

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent intent)

if (mUploadMessagesAboveL !=null)

onActivityResultAboveL(requestCode, resultCode, intent);

   

if (mUploadMessage ==null)return;

    Uri uri =null;

    if (requestCode ==REQUEST_CAMERA && resultCode ==RESULT_OK)

uri =cameraUri;

        Log.e("onActivityResult: " + uri.toString());

   

if (requestCode ==REQUEST_CHOOSE && resultCode ==RESULT_OK)

uri = afterChosePic(intent);

   

mUploadMessage.onReceiveValue(uri);

    mUploadMessage =null;

    super.onActivityResult(requestCode, resultCode, intent);



/**

* 5.0以后机型 返回文件选择

*

* @param requestCode

* @param resultCode

* @param data

*/

private void onActivityResultAboveL(int requestCode, int resultCode, Intent data)

Uri[] results =null;

    if (requestCode ==REQUEST_CAMERA && resultCode ==RESULT_OK)

results =new Uri[]cameraUri;

   

if (requestCode ==REQUEST_CHOOSE && resultCode ==RESULT_OK)

if (data !=null)

String dataString = data.getDataString();

            if (dataString !=null)

results =new Uri[]Uri.parse(dataString);

       



mUploadMessagesAboveL.onReceiveValue(results);

    mUploadMessagesAboveL =null;

return;



基本都可以拿去用了,希望有帮助

以上是关于在Android系统中怎样用WebView打开一些非HTTP开头的网址的主要内容,如果未能解决你的问题,请参考以下文章

用webview做的android客户端怎样调用摄像头拍照

Android webview加载的页面怎样调试JS

error inflating class android.webkit.webview怎样解决

怎么设置webview内容大小

怎样使用webview打开每个网页

Android WebView 调用系统拍照和相册