[英]Android App- Popup Menu with the Camera App
I've been trying to get my first Android app to work. 我一直试图让我的第一个Android应用程序正常工作。 I have one activity that uses a webview, and I use it to open web pages that have html forms on them.
我有一个使用webview的活动,我用它来打开上面有html表单的网页。
There was some trouble getting the "Choose File" button (for file inputs) to work, but I finally solved it thanks to the help posted here File Upload in WebView . 使“选择文件”按钮(用于文件输入)无法正常工作,但是由于在WebView中的File Upload中发布的帮助,我终于解决了它。 From there, I'm pretty much using the Main Activity java code they made available on Github.
从那里开始,我几乎使用了他们在Github上可用的Main Activity java代码 。
My actual problem is that when clicking a file input button, I don't get the option for the user to use the device's Camera , which I'd like to have. 我的实际问题是,单击文件输入按钮时,我没有用户想要使用设备的Camera的选项 。 At first I thought it might be related to having to ask for Camera permission for the app, but I implemented it and I was wrong on that one.
起初,我认为这可能与必须为该应用程序请求“相机”权限有关,但是我实现了它,但在那个应用程序上我错了。 The problem here is I'm unexperienced with the Intents to get the popup menus, like:
这里的问题是我对获取弹出菜单的意图缺乏经验,例如:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Some guidance on finding a way to get a "Camera" option to come up would be much appreciated. 我们非常感谢您提供一些有关找到“相机”选项的方法的指导。
Let me show you what I mean by this, opening the same html form on Chrome, and my app on 2 different Android OS versions (4.4.4 and 6.0). 让我向您展示我的意思,在Chrome上打开相同的html表单,并在2个不同的Android OS版本(4.4.4和6.0)上打开我的应用。 Using my Samsung Galaxy Tab, running Android 4.4.4.
使用运行Android 4.4.4的 Samsung Galaxy Tab 。 When opening a page that has an html form, on Google Chrome , clicking the Choose File button, I get this menu
当打开具有html表单的页面时,在Google Chrome上 ,单击“ 选择文件”按钮, 我会看到此菜单
That is what I want to have in my app 那就是我想要在我的应用程序中拥有的
GETing using the same URL and displaying it in my App (on 4.4.4) , using my webview, when clicking the Choose File button, I get this menu 使用相同的URL并将其显示在我的App中(在4.4.4上) ,使用Webview,单击“ 选择文件”按钮时,出现此菜单
(Also, I've tried clicking that Choose File button on my app, on an Android 6.0 emulator , and it goes straight to the gallery , and there's no Camera option there): (此外,我尝试在Android 6.0模拟器上的应用程序上单击“选择文件”按钮, 它直接进入Gallery ,并且那里没有Camera选项):
This is the relevant part of the code: 这是代码的相关部分:
//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;
}
This is a helper function to offer both a Camera and Media Picker chooser intent. 这是一个辅助功能,可同时提供“相机”和“媒体选择器”选择器意图。 I think you're specifically asking about ACTION_IMAGE_CAPTURE, ie the first part of this sample code snippet.
我认为您是在特别询问ACTION_IMAGE_CAPTURE,即此示例代码片段的第一部分。
private Intent getPhotoChooserIntent(String acceptType, String capture)
{
try
{
//---------------------------------------------------------------------
// camera Intent
//---------------------------------------------------------------------
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HHmmss", Locale.US);
// path to picture
File dirPhotos = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File photo = new File(String.format("%s/Photo_%s.jpg", dirPhotos.getAbsolutePath(), sdf.format(cal.getTime())));
mPhoto = Uri.fromFile(photo);
intentCamera.putExtra (MediaStore.EXTRA_OUTPUT, mPhoto);
// pass "camera" in this parameter for a Camera only picker
if (capture.equalsIgnoreCase("camera"))
{
if (intentCamera.resolveActivity(mContext.getPackageManager()) != null)
return (intentCamera);
return (null);
}
//---------------------------------------------------------------------
// media picker Intent
//---------------------------------------------------------------------
Intent intentPicker = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// aggregate list of resolved intents
List<Intent> intentsList = new ArrayList<>();
List<ResolveInfo> resInfo = mContext.getPackageManager().queryIntentActivities(intentPicker, 0);
for (ResolveInfo resolveInfo : resInfo)
{
Intent intentTarget = new Intent(intentPicker);
intentTarget.setPackage(resolveInfo.activityInfo.packageName);
intentsList.add(intentTarget);
}
if (intentCamera.resolveActivity(mContext.getPackageManager()) != null)
intentsList.add(intentCamera);
if (intentsList.size() > 0)
{
Intent intentChooser = Intent.createChooser(intentsList.remove(intentsList.size() - 1), mContext.getResources().getString(R.string.mediapicker));
intentChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentsList.toArray(new Parcelable[]{}));
return (intentChooser);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return (null);
}
Assumes mContext = activity context. 假设mContext =活动上下文。 mPhoto is a class variable of type Uri, used for accessing the picture acquired from the camera in your onActivityResult handler.
mPhoto是类型为Uri的类变量,用于访问onActivityResult处理程序中从相机获取的图片。
Hope this helps! 希望这可以帮助!
看看Mario Velasco的 回答是否对您有帮助。
Ok, so I've figured out part of the problem in the meantime. 好的,因此我已经找出了部分问题。
I had stated the needed permissions in the manifest, like: 我已经在清单中声明了所需的权限,例如:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
But, I made a begginer mistake by not asking explicitly for access to the device's camera and storage, as in: 但是,我犯了一个开始的错误,因为没有明确要求访问设备的相机和存储,如:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
The part for Android 5.0+ works (the code was ok to begin with), and there's a menu where you can choose between the camera or pics from the gallery. 适用于Android 5.0+的部分可以正常工作(代码可以开始使用),并且有一个菜单,您可以在其中选择相机或画廊中的照片。
Either way, I've also tested user CSmith's suggestion for 5.0+ and confirmed it also works, so if anyone ever runs into this, it's a viable alternative you can try. 无论哪种方式,我都已经测试了用户CSmith对于5.0+的建议,并确认它也可以使用,因此,如果有人遇到这种情况,可以尝试一下。
For Android versions older than 5, I've managed to make clicking the "Choose file" (but no gallery option) button open the Camera app with some changes, like this: 对于5之前的Android版本,我设法单击“选择文件”(但没有图库选项)按钮,打开相机应用程序并进行了一些更改,如下所示:
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUM = uploadMsg;
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 1);
}
And I'm using this "fix" for 3 and 4, for now. 现在,我将这个“修复”用于3和4。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.