【发布时间】:2017-01-30 17:21:53
【问题描述】:
我一直在努力让我的第一个 Android 应用程序运行起来。我有一个使用 webview 的活动,我用它来打开上面有 html 表单的网页。
让“选择文件”按钮(用于文件输入)工作时遇到了一些问题,但感谢File Upload in WebView 此处发布的帮助,我终于解决了。 从那里开始,我几乎使用他们在 Github 上提供的 Main Activity java code。
我的实际问题是,当单击文件输入按钮时,我没有让用户使用我想要的设备相机的选项。起初我认为这可能与必须为应用程序请求相机许可有关,但我实现了它,但我错了。这里的问题是我不熟悉 Intents 来获取弹出菜单,例如:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
非常感谢您提供有关如何找到“相机”选项的指导。
让我向您展示我的意思,在 Chrome 上打开相同的 html 表单,并在 2 个不同的 Android 操作系统版本(4.4.4 和 6.0)上打开我的应用程序。使用我的三星 Galaxy Tab,运行 Android 4.4.4。在 Google Chrome 上打开一个包含 html 表单的页面时,点击 Choose File 按钮,I get this menu
这就是我想在我的应用中拥有的东西
使用相同的 URL 获取并显示它在我的应用程序中(在 4.4.4 上),使用我的 web 视图,当点击 Choose File 按钮时,I get this menu
(另外,我尝试在我的应用上,在 Android 6.0 模拟器上点击选择文件按钮,and it goes straight to the gallery,但那里没有相机选项):
这是代码的相关部分:
//For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUM = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult(Intent.createChooser(i, "Choose an Image please"), MainActivity.FCR);
}
//For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams){
if(mUMA != null){
mUMA.onReceiveValue(null);
}
mUMA = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){
File photoFile = null;
try{
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCM);
}catch(IOException ex){
Log.e(TAG, "Image file creation failed", ex);
}
if(photoFile != null){
mCM = "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, FCR);
return true;
}
【问题讨论】:
标签: java android android-intent webview android-camera