android - WebView kotlin 上传文件摄像头和存储
问题描述
我有一个可用于从 SD 上传文件的 webview,但我无法获取上传来询问我是通过相机还是 SD。
寻找 java 的例子,这是唯一简单而实用的例子,但我没有设法将它迁移到 kotlin
//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;
}
});
我在 getActivity(), tryArray = new Intent[0]; 中有一个错误,我无法解决。
非常感谢
解决方案
推荐阅读
- php - 如何将我在 AWS 云中的 php 项目与我的本地 Oracle 数据库连接起来?
- c# - 如何在二维数组中声明和存储值?
- cmd - JMeter 非 gui 模式不显示脚本的统计信息,但它确实适用于其他脚本
- javascript - Firebase:第一次渲染时未设置用户
- android - 如何将“均衡器”与“audio_service”包一起使用
- delphi - 如何将自定义属性添加到 FireMonkey 中动态(以编程方式)创建的控件?
- window-functions - 使用lookML计算looker中同一列的时间差
- oracle - 使用 PutDatabaseRecord 处理器进行非常慢的更新
- r - 更改选项中的名称而不影响引用列
- python - 运算符或函数的操作数类型不正确;运算符:ADD,操作数类型:STRING,typeSet:ALLOWED_FOR_ADD_OPERAND