1. 程式人生 > >H5呼叫Android圖片上傳功能

H5呼叫Android圖片上傳功能

public class OpenFileWebChromeClient extends WebChromeClient {
    public static final int REQUEST_FILE_PICKER = 1;
    public ValueCallback<Uri> mFilePathCallback;
    public ValueCallback<Uri[]> mFilePathCallbacks;
Activity mContext;
    public OpenFileWebChromeClient(Activity mContext){
        super
(); this.mContext = mContext; } // Android < 3.0 呼叫這個方法 public void openFileChooser(ValueCallback<Uri> filePathCallback) { mFilePathCallback = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"
); mContext.startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); } // 3.0 + 呼叫這個方法 public void openFileChooser(ValueCallback filePathCallback, String acceptType) { mFilePathCallback = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT
); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); mContext.startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); } // / js上傳檔案的<input type="file" name="fileField" id="fileField" />事件捕獲 // Android > 4.1.1 呼叫這個方法 public void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) { mFilePathCallback = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); mContext.startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); } @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { mFilePathCallbacks = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); mContext.startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); return true; } }
用Intent去呼叫檔案管理器之後會將檔案帶回,用onActivityResult來接收
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == OpenFileWebChromeClient.REQUEST_FILE_PICKER) {
        if (mOpenFileWebChromeClient.mFilePathCallback != null) {
            Uri result = data == null || resultCode != Activity.RESULT_OK ? null
: data.getData();
            if (result != null) {
                String path = MediaUtility.getPath(getApplicationContext(),
result);
Uri uri = Uri.fromFile(new File(path));
mOpenFileWebChromeClient.mFilePathCallback
                        .onReceiveValue(uri);
} else {
                mOpenFileWebChromeClient.mFilePathCallback
                        .onReceiveValue(null);
}
        }
        if (mOpenFileWebChromeClient.mFilePathCallbacks != null) {
            Uri result = data == null || resultCode != Activity.RESULT_OK ? null
: data.getData();
            if (result != null) {
                String path = MediaUtility.getPath(getApplicationContext(),
result);
Uri uri = Uri.fromFile(new File(path));
mOpenFileWebChromeClient.mFilePathCallbacks
                        .onReceiveValue(new Uri[]{uri});
} else {
                mOpenFileWebChromeClient.mFilePathCallbacks
                        .onReceiveValue(null);
}
        }

        mOpenFileWebChromeClient.mFilePathCallback = null;
mOpenFileWebChromeClient.mFilePathCallbacks = null;
}
}

這樣應該就可以了, 轉載宣告:文章內容摘自: