【发布时间】:2016-07-01 13:52:44
【问题描述】:
我正在为 Android 开发一个包装应用程序,需要连接到相机/照片库(例如,用户可以上传自己的照片)。我发现了很多关于这个主题的帖子,包括Upload an Image from camera or gallery in WebView 和Upload camera photo and filechooser from webview INPUT field,但是我根据这些解决方案建模的代码不起作用。我在 openFileChooser 函数中放置了一个打印语句,当在我的 webview 中点击添加照片/文档 Javascript 链接时,它不会被触发。任何建议将不胜感激,我将我的代码包括在下面:
private Uri mCapturedImageURI = null;
//make HTML upload button work in Webview
private ValueCallback<Uri> mUploadMessage;
private String filePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in_page);
Intent currentIntent = getIntent();
String action = currentIntent.getAction();
String url;
//check for permissions
boolean camera = checkCameraPermissions();
boolean location = checkLocationPermissions();
boolean photos = checkPhotoPermissions();
if (!location){
ActivityCompat.requestPermissions(this,
new String[] {"android.permission.ACCESS_FINE_LOCATION"}, REQUEST_LOCATION);
requested_location = true;
}
if (!camera ) {
ActivityCompat.requestPermissions(this,
new String[] {"android.permission.CAMERA"}, REQUEST_CAMERA);
requested_camera = true;
}
if (!photos) {
ActivityCompat.requestPermissions(this,
new String[] {"android.permission.READ_EXTERNAL_STORAGE"}, REQUEST_PHOTOS);
requested_photos = true;
}
url = Constants.login_page;
//start up the webview, initiate navigation client
myWebView = (WebView) findViewById(R.id.webview);
if (myWebView != null) {
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setAllowFileAccessFromFileURLs(true);
myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setSupportMultipleWindows(true);
myWebView.getSettings().setGeolocationEnabled(true);
myWebView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType){
this.openFileChooser(uploadMsg);
}
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
this.openFileChooser(uploadMsg);
}
private Uri imageUri;
public void openFileChooser(ValueCallback<Uri> uploadMsg ) {
// Update message
mUploadMessage = uploadMsg;
System.out.println("we are here");
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "my_app");
filePath = imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
File file = new File(filePath);
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
LogInPage.this.startActivityForResult(captureIntent, REQUEST_PHOTOS);
}
});
//myWebView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");
myWebView.loadUrl(url);
myWebView.setWebViewClient(new NavigationClient());
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if ((requestCode == SELECT_PHOTO) ||(requestCode == REQUEST_CAMERA)) {
if (null == this.mUploadMessage || (resultCode != RESULT_OK && !new File(filePath).exists())) {
this.mUploadMessage.onReceiveValue(null);
} else {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, this.filePath);
this.mUploadMessage.onReceiveValue(this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values));
}
this.mUploadMessage = null;
}
}
【问题讨论】:
标签: android webview camera gallery