Android WebView kotlin上传文件摄像机和存储
我有一个webview,可以从SD上传文件,但我一直无法获得上传文件来询问我是通过摄像头还是SD上传的 寻找java的例子,java是唯一简单实用的例子,但我没有将它迁移到kotlinAndroid WebView kotlin上传文件摄像机和存储,android,webview,kotlin,Android,Webview,Kotlin,我有一个webview,可以从SD上传文件,但我一直无法获得上传文件来询问我是通过摄像头还是SD上传的 寻找java的例子,java是唯一简单实用的例子,但我没有将它迁移到kotlin //用于Android5.0+ 公共布尔onShowFileChooser( WebView WebView,ValueCallback filePathCallback, 文件选择器参数(文件选择器参数){ if(mFilePathCallback!=null){ mFilePathCallback.onRe
//用于Android5.0+
公共布尔onShowFileChooser(
WebView WebView,ValueCallback filePathCallback,
文件选择器参数(文件选择器参数){
if(mFilePathCallback!=null){
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback=filePathCallback;
Intent takePictureIntent=新的意图(MediaStore.ACTION\u IMAGE\u CAPTURE);
if(takePictureContent.resolveActivity(getActivity().getPackageManager())!=null){
//创建照片应该放在哪里的文件
文件photoFile=null;
试一试{
photoFile=createImageFile();
takePictureContent.putExtra(“光路”,mCameraPhotoPath);
}捕获(IOEX异常){
//创建文件时出错
Log.e(标记“无法创建图像文件”,例如);
}
//仅当成功创建文件时才继续
if(photoFile!=null){
mCameraPhotoPath=“文件:”+photoFile.getAbsolutePath();
takePictureContent.putExtra(MediaStore.EXTRA_输出,
fromFile(photoFile));
}否则{
takePictureContent=null;
}
}
意图内容SelectionContent=新意图(Intent.ACTION\u GET\u CONTENT);
ContentSelectionContent.addCategory(Intent.CATEGORY\u可打开);
contentSelectionContent.setType(“image/*”);
意图[]意图射线;
if(takePictureContent!=null){
intentArray=newintent[]{takePictureIntent};
}否则{
intentArray=新意图[0];
}
意图选择器content=新意图(Intent.ACTION\u选择器);
选择content.putExtra(Intent.EXTRA\u Intent,content selection内容);
选择content.putExtra(Intent.EXTRA_标题,“图像选择器”);
选择content.putExtra(Intent.EXTRA\u INITIAL\u INTENTS,intentArray);
startActivityForResult(选择内容、请求代码和LOLIPOP);
返回true;
}
});
getActivity()中有错误,tryArray=new Intent[0];,我无法解决
非常感谢您在哪里呼叫
getActivity()
,tryArray=newintent[0]代码>,抛出的错误是什么?-if(takePictureContent.resolveActivity(getActivity().getPackageManager())!=null)使用提供的参数无法调用以下函数:public open fun getActivity(p0:Context!、p1:Int、p2:Intent!、p3:Int):PendingEvent!在android.app.pendingent public open fun getActivity(p0:Context!、p1:Int、p2:Intent!、p3:Int、p4:Bundle!)中定义:pendingent!在android.app.pendingent中定义
-intentArray=arrayOfNulls(0)类型不匹配:推断类型为数组,但应为数组
尝试放置在getActivity()
之后使用code>,这样它就不会返回一个可为null的值,对于第二个值,出于同样的原因使用arrayOf()
。更新:1。getActivity().getPackageManager()-->this@MyActivity.getPackageManager() 2. intentArray=新意图[0];-->intentArray=arrayOf()谢谢urgentx
//For Android5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Unable to create Image File", ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, REQUEST_CODE_LOLIPOP);
return true;
}
});