Webview调用本地相册

本文介绍了一个定制化的WebChromeClient子类,用于处理不同Android版本中WebView组件调用文件选择器的兼容性问题。该类根据不同API级别支持了从3.0到5.0及以上版本的文件选择回调,确保应用在不同设备上的一致体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import android.net.Uri;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;


public class ReWebChomeClient extends WebChromeClient {

    private OpenFileChooserCallBack mOpenFileChooserCallBack;

    public ReWebChomeClient(OpenFileChooserCallBack openFileChooserCallBack) {
        mOpenFileChooserCallBack = openFileChooserCallBack;
    }

    //For Android 3.0+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        MyLog.d("ttt","webview调用相册3.0+");
        mOpenFileChooserCallBack.openFileChooserCallBack(uploadMsg, acceptType);
    }

    // For Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        MyLog.d("ttt","webview调用相册3.0-");
        openFileChooser(uploadMsg, "");
    }

    // For Android  > 4.1.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        MyLog.d("ttt","webview调用相册4.1.1+");
        openFileChooser(uploadMsg, acceptType);
    }

    public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
       // openFileChooserImplForAndroid5(uploadMsg);
        MyLog.d("ttt","webview调用相册5.0+");
        mOpenFileChooserCallBack.onShowFileChooserCallBack(uploadMsg);
        return true;
    }

    public interface OpenFileChooserCallBack {
        void openFileChooserCallBack(ValueCallback<Uri> uploadMsg, String acceptType);
        void onShowFileChooserCallBack(ValueCallback<Uri[]> uploadMsg);
    }


}

public void showOptions() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setOnCancelListener(new ReOnCancelListener());
    alertDialog.setTitle(R.string.options);
    alertDialog.setItems(R.array.options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == 0) {
                        mSourceIntent = ImageUtil.choosePicture();
                        startActivityForResult(mSourceIntent, REQUEST_CODE_PICK_IMAGE);
                    } else {
                        mSourceIntent = ImageUtil.takeBigPicture();
                        startActivityForResult(mSourceIntent, REQUEST_CODE_IMAGE_CAPTURE);
                    }
                }
            }
    );
    alertDialog.show();
}
public static Intent choosePicture() {
    Intent intent = new Intent( Intent.ACTION_PICK,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    return Intent.createChooser(intent, null);
}


public static Intent takeBigPicture() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, newPictureUri(getNewPhotoPath()));
    return intent;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值