CN: 解决Android 4.3 和 4.4 系列中H5不能发起文件上传的BUG

Solution: Using Crosswalk from Intel Open Source

Ref code: https://github.com/dougdiego/CrosswalkDemo

Ref Article: 

Crosswalk入门

http://stackoverflow.com/questions/19882331/html-file-input-in-android-webview-android-4-4-kitkat

http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework

And I’ll post the full code later to github, please wait.

Key Code:

package org.diego.android.crosswalkdemo;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.util.Log;

import android.webkit.ValueCallback;

import org.xwalk.core.XWalkPreferences;

import org.xwalk.core.XWalkResourceClient;

import org.xwalk.core.XWalkUIClient;

import org.xwalk.core.XWalkView;

import java.io.File;

public class XWalkWebViewActivity extends Activity {

    class UIClient extends XWalkUIClient {

        public UIClient(XWalkView xwalkView) {

            super(xwalkView);

        }

        public void openFileChooser(XWalkView view,

                                    ValueCallback uploadFile, String acceptType, String capture) {

            super.openFileChooser(view, uploadFile, acceptType, capture);

            mFilePathCallback = uploadFile;

            Log.d(“fchooser”, “Opened file chooser.”);

        }

    }

    private XWalkView xWalkWebView;

    private ValueCallback mFilePathCallback;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_xwalk_web_view);

        xWalkWebView=(XWalkView)findViewById(R.id.xwalkWebView);

        xWalkWebView.setUIClient(new UIClient(xWalkWebView));

        xWalkWebView.load(“http://www.google.com”, null);

        // turn on debugging

        XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);

    

    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        super.onActivityResult(requestCode, resultCode, intent);

        if (xWalkWebView != null) {

            if (mFilePathCallback != null) {

                Uri result = intent == null   resultCode != Activity.RESULT_OK ? null

                        : intent.getData();

                if (result != null) {

                    File file = ImageUtils.getPhotoFromResult(this, intent);

//                    String path = ImageUtils.getRealPathFromURI(this, result);

                    System.out.println(“上传。。。” + file);

                    Uri uri = Uri.fromFile(file);

                    mFilePathCallback.onReceiveValue(uri);

                } else {

                    mFilePathCallback.onReceiveValue(null);

                }

            }

            mFilePathCallback = null;

        }

//        xWalkWebView.onActivityResult(requestCode, resultCode, intent); // 加了浏览器就说重复调用, 会崩溃

    }

    @Override

    protected void onPause() {

        super.onPause();

        if (xWalkWebView != null) {

            xWalkWebView.pauseTimers();

            xWalkWebView.onHide();

        }

    }

    @Override

    protected void onResume() {

        super.onResume();

        if (xWalkWebView != null) {

            xWalkWebView.resumeTimers();

            xWalkWebView.onShow();

        }

    }

    @Override

    protected void onDestroy() {

        super.onDestroy();

        if (xWalkWebView != null) {

            xWalkWebView.onDestroy();

        }

    }

}

To reduce the app file path, please add following code to build.grade:

    productFlavors {

        armv7 {

            ndk {

                abiFilters “armeabi-v7a”, “”

            }

        }

        x86 {

            ndk {

                abiFilters “x86”, “”

            }

        }

    }

转载请注明:WebLogic Android 博客 » Solve the html file upload function in android web view 4.4