X5WebView+PictureSelector多图片选择功能遇到的问题

先上图片和代码(其实应该搞个gif的,不过大家都是有经验的程序员,图片+代码就能理解对吧)

private var mUploadMessage: ValueCallback? = null
private var mUploadCallbackAboveL: ValueCallback>? = null

inner class WebViewClients : WebChromeClient() {

    override fun onProgressChanged(webView: WebView?, newProgress: Int) {
        pb!!.progress = newProgress
        if (newProgress == 100) {
            pb!!.visibility = View.GONE
        }
        super.onProgressChanged(webView, newProgress)
    }

    override fun onReceivedTitle(webView: WebView?, s: String?) {
        super.onReceivedTitle(webView, s)
        if (TextUtils.isEmpty(title)) {
            widget_title!!.text = wv!!.title
        } else {
            widget_title!!.text = title
        }
        title = wv!!.title
    }

    //4.1
    override fun openFileChooser(uploadMsg: ValueCallback, acceptType: String, capture: String) {

        openFileChooserImpl(uploadMsg)
    }

    //5.1
    override fun onShowFileChooser(mWebView: WebView, filePathCallback: ValueCallback>, fileChooserParams: WebChromeClient.FileChooserParams): Boolean {
        openFileChooserImplForAndroid5(filePathCallback)
        return true
    }
}

private fun openFileChooserImplForAndroid5(uploadMsg: ValueCallback>) {
    mUploadCallbackAboveL = uploadMsg
    showWayChooseDialog()
}

//5.0以下的掉用
private fun openFileChooserImpl(uploadMsg: ValueCallback) {
    mUploadMessage = uploadMsg
    showWayChooseDialog()
}

private var dialog: Dialog? = null
private fun showWayChooseDialog() {
    dialog = Dialog(this)
    dialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog!!.show()
    dialog!!.setContentView(R.layout.dialog_my_way)
    val dialogWindow = dialog!!.window
    dialogWindow!!.setBackgroundDrawableResource(R.color.transparent)
    val attributes = dialogWindow.attributes
    attributes.width = ViewPager.LayoutParams.MATCH_PARENT
    attributes.height = ViewPager.LayoutParams.WRAP_CONTENT
    attributes.gravity = Gravity.BOTTOM
    dialog!!.window!!.attributes = attributes

    dialog!!.setCanceledOnTouchOutside(false)

    dialog!!.tv_video1.text = "拍照"

    dialog!!.tv_photo.setOnClickListener(View.OnClickListener {
        dialog!!.dismiss()
        PictureSelectManager.selectPicture().openPhoto(this@WebViewActivity, resultCallback)
    })
    dialog!!.tv_video.setOnClickListener(View.OnClickListener {
        dialog!!.dismiss()
        PictureSelectManager.selectPicture().openPhoto(this@WebViewActivity, resultCallback)
    })
    dialog!!.tv_record.setOnClickListener(View.OnClickListener {
        dialog!!.dismiss()
        val intent = Intent(this, RecordActivity::class.java)
        intent.putExtra("pageUrl", "http://www.baidu.com/")
        startActivity(intent)
    })
    dialog!!.ll_dismiss.setOnClickListener(View.OnClickListener {
        resultCallback.onSelectSuccess(var1);
        dialog!!.dismiss() })
}

private val resultCallback = PictureConfig.OnSelectResultCallback { resultList ->

    if (resultList != null) {
        val file = File(resultList[0].compressPath)
        val localUri = Uri.fromFile(file)
        val localIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri)
        sendBroadcast(localIntent)
        if (mUploadCallbackAboveL != null) {
            mUploadCallbackAboveL!!.onReceiveValue(arrayOf(Uri.fromFile(file)))
            mUploadCallbackAboveL = null
        }
        if (mUploadMessage != null) {
            mUploadMessage!!.onReceiveValue(Uri.fromFile(file)!!)
            mUploadMessage = null
        }
    }
}
复制代码

点击+号弹出选择框,当前代码的问题就是,如果点击取消,或者到图片选择界面点击返回和取消后,再次点击+号,则不会弹出选择框。

然后bug分配给我了(代码是接手前任写的,痛苦中。。),第一时间先测试了web回调接口,我靠居然没有回调(一整小兴奋,可以甩锅给前端),不过兴奋过后想不对啊,为什么选择图片后就不会出现这样的bug呢?那情况应该还是处在了Android端。还是好好想想吧。那选择图片和不选择图片操作有什么差距吗?想到这里第一时间去看PictureSelector的回调方法

private val resultCallback = PictureConfig.OnSelectResultCallback { resultList ->

    if (resultList != null) {
        val file = File(resultList[0].compressPath)
        if (mUploadCallbackAboveL != null) {
            mUploadCallbackAboveL!!.onReceiveValue(arrayOf(Uri.fromFile(file)))
            mUploadCallbackAboveL = null
        }
        if (mUploadMessage != null) {
            mUploadMessage!!.onReceiveValue(Uri.fromFile(file)!!)
            mUploadMessage = null
        }
    }
}
复制代码

果然没有图片的时候没有处理,哈哈哈哈,直接

 else{
        
        if (mUploadCallbackAboveL != null) {
            mUploadCallbackAboveL!!.onReceiveValue(null)
            mUploadCallbackAboveL = null
        }
        if (mUploadMessage != null) {
            mUploadMessage!!.onReceiveValue(null)
            mUploadMessage = null
        } 
 }
复制代码

又是一通小兴奋(心里大笑,此等小问题岂能难到我),部署测试,what???? 居然不行(心里跑过一群羊驼),不应该啊?不过现实就是这样,老老实实的在找问题吧。不过我猜是回调没有执行,直接在if前面加了Log,看下有没有回调,原来如此,取消没有回调。 打开PictureSelector源码发现

打开图片选择页面

public void openPhoto(Context mContext, PictureConfig.OnSelectResultCallback resultCall) {
    if (!Utils.isFastDoubleClick()) {
        if (this.options == null) {
            this.options = (new Builder()).create();
        }

        Intent intent = new Intent(mContext, PictureImageGridActivity.class);
        intent.putExtra("function_options", this.options);
        mContext.startActivity(intent);
        ((Activity)mContext).overridePendingTransition(anim.slide_bottom_in, 0);
        this.resultCallback = resultCall;
    }
}
复制代码

取消和返回按钮代码

 public void onClick(View view) {
    Intent intent = new Intent();
    int id = view.getId();
    if (id == id.picture_left_back) {
        this.activityFinish(1);
    } else if (id == id.picture_tv_right) {
        this.activityFinish(2);
    }
    .......
 }

 private void activityFinish(int type) {
    switch(type) {
    case 1:
        List selectedImages = this.adapter.getSelectedImages();
        if (selectedImages == null) {
            selectedImages = new ArrayList();
        }

        this.startActivity((new Intent(this.mContext, PictureAlbumDirectoryActivity.class)).putExtra("previewSelectList", (Serializable)selectedImages).putExtra("function_options", this.options));
        this.overridePendingTransition(anim.slide_right_in, anim.slide_right_out);
        ImagesObservable.getInstance().notifySelectLocalMediaObserver((List)selectedImages);
        this.finish();
        break;
    case 2:
        this.clearData();
        EventEntity obj = new EventEntity(2773);
        EventBus.getDefault().post(obj);
        this.finish();
        this.overridePendingTransition(0, anim.slide_bottom_out);
    }

}
复制代码

发现取消和返回是不会执行回调接口的,只是finish掉当前界面,这怎么办? 测试小(老)姐(八)姐(婆)又催了,突然灵光一闪....

选择框“取消”按钮代码

 dialog!!.ll_dismiss.setOnClickListener(View.OnClickListener {
        resultCallback.onSelectSuccess(var1);
        dialog!!.dismiss() })
复制代码

重写onResume

var var1:List?=null
override fun onResume() {
    super.onResume()
    resultCallback.onSelectSuccess(var1);
}
复制代码

修改resultCallback

private val resultCallback = PictureConfig.OnSelectResultCallback { resultList ->

    if (resultList != null) {
        var1=resultList;
        val file = File(resultList[0].compressPath)
        val localUri = Uri.fromFile(file)
        val localIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri)
        sendBroadcast(localIntent)
        if (mUploadCallbackAboveL != null) {
            mUploadCallbackAboveL!!.onReceiveValue(arrayOf(Uri.fromFile(file)))
            mUploadCallbackAboveL = null
        }
        if (mUploadMessage != null) {
            mUploadMessage!!.onReceiveValue(Uri.fromFile(file)!!)
            mUploadMessage = null
        }
    }else{
        if (mUploadCallbackAboveL != null) {
            mUploadCallbackAboveL!!.onReceiveValue(null)
            mUploadCallbackAboveL = null
        }
        if (mUploadMessage != null) {
            mUploadMessage!!.onReceiveValue(null)
            mUploadMessage = null
        }
    }
}
复制代码

部署测试,OK搞定,虽然问题解决了,但我感觉方式并不是很好,希望有好的解决办法的小伙伴,把办法发给我学习一下

[email protected]

转载于:https://juejin.im/post/5ba08fee5188255c681575a7

你可能感兴趣的:(X5WebView+PictureSelector多图片选择功能遇到的问题)