public class FileChooser {
private static FileChooser FILE_CHOOSER;
private Activity mActivity;
private final int FILE_SELECT_CODE = 1;
private ValueCallback<Uri> mUploadCallbackBelow;
private ValueCallback<Uri[]> mUploadCallbackAboveL;
public static FileChooser get(Activity activity){
if (FILE_CHOOSER == null){
FILE_CHOOSER = new FileChooser(activity);
}
return FILE_CHOOSER;
}
public FileChooser(Activity activity) {
mActivity = activity;
}
/**
* API >= 21(Android 5.0.1)
*/
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
mUploadCallbackAboveL = filePathCallback;
takeImage();
return true;
}
/**
* 16(Android 4.1.2) <= 20(Android 4.4W.2)
*/
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadCallbackBelow = uploadMsg;
takeImage();
}
private void takeImage() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
mActivity.startActivityForResult(Intent.createChooser(i, "File Browser"), FILE_SELECT_CODE);
}
// 接收图片回调
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILE_SELECT_CODE && resultCode == RESULT_OK && data != null) {
if (mUploadCallbackBelow != null) {
chooseBelow(resultCode, data);
} else if (mUploadCallbackAboveL != null) {
chooseAbove(resultCode, data);
}
}
}
/**
* Android API < 21(Android 5.0)
*/
private void chooseAbove(int resultCode, Intent data) {
Uri uriData = data.getData();
if (uriData != null) {
mUploadCallbackAboveL.onReceiveValue(new Uri[]{uriData});
} else {
mUploadCallbackAboveL.onReceiveValue(null);
}
}
/**
* 16(Android 4.1.2) <= 20(Android 4.4W.2)
*/
private void chooseBelow(int resultCode, Intent data) {
Uri uri = data.getData();
if (uri != null) {
mUploadCallbackBelow.onReceiveValue(uri);
} else {
mUploadCallbackBelow.onReceiveValue(null);
}
}
}
HTML调用原生相册
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在此之前,最好了解一下RN与OC直接的通信机制! 1.引入头文件#import <RCTBridgeModule....
- 最近项目里面有有个商品活动界面,要与web端传值,将用户在网页点击的商品id 传给客户端,也就是js交互,其实再说...