私はAndroidのラッパーアプリケーションを開発中で、カメラ/フォトギャラリーに接続する必要があります(ユーザーが自分の写真をアップロードできるように)。私はUpload an Image from camera or gallery in WebViewとUpload camera photo and filechooser from webview INPUT fieldを含む多くの記事を見つけましたが、これらのソリューションの後にモデル化したコードは機能しません。私はopenFileChooser関数の中にprintステートメントを置いていますが、写真やドキュメントのJavascriptリンクを追加するとwebviewでヒットするとトリガーされません。任意の提案は、私は以下の私のコードを含むよ、高く評価されるだろう:写真/ドキュメントアップロードAndroid Webview
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;
}
}