[英]Android App- Popup Menu with the Camera App
我一直试图让我的第一个Android应用程序正常工作。 我有一个使用webview的活动,我用它来打开上面有html表单的网页。
使“选择文件”按钮(用于文件输入)无法正常工作,但是由于在WebView中的File Upload中发布的帮助,我终于解决了它。 从那里开始,我几乎使用了他们在Github上可用的Main Activity java代码 。
我的实际问题是,单击文件输入按钮时,我没有用户想要使用设备的Camera的选项 。 起初,我认为这可能与必须为该应用程序请求“相机”权限有关,但是我实现了它,但在那个应用程序上我错了。 这里的问题是我对获取弹出菜单的意图缺乏经验,例如:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
我们非常感谢您提供一些有关找到“相机”选项的方法的指导。
让我向您展示我的意思,在Chrome上打开相同的html表单,并在2个不同的Android OS版本(4.4.4和6.0)上打开我的应用。 使用运行Android 4.4.4的 Samsung Galaxy Tab 。 当打开具有html表单的页面时,在Google Chrome上 ,单击“ 选择文件”按钮, 我会看到此菜单
那就是我想要在我的应用程序中拥有的
使用相同的URL并将其显示在我的App中(在4.4.4上) ,使用Webview,单击“ 选择文件”按钮时,出现此菜单
(此外,我尝试在Android 6.0模拟器上的应用程序上单击“选择文件”按钮, 它直接进入Gallery ,并且那里没有Camera选项):
这是代码的相关部分:
//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;
}
这是一个辅助功能,可同时提供“相机”和“媒体选择器”选择器意图。 我认为您是在特别询问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);
}
假设mContext =活动上下文。 mPhoto是类型为Uri的类变量,用于访问onActivityResult处理程序中从相机获取的图片。
希望这可以帮助!
看看Mario Velasco的 回答是否对您有帮助。
好的,因此我已经找出了部分问题。
我已经在清单中声明了所需的权限,例如:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
但是,我犯了一个开始的错误,因为没有明确要求访问设备的相机和存储,如:
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);
}
}
适用于Android 5.0+的部分可以正常工作(代码可以开始使用),并且有一个菜单,您可以在其中选择相机或画廊中的照片。
无论哪种方式,我都已经测试了用户CSmith对于5.0+的建议,并确认它也可以使用,因此,如果有人遇到这种情况,可以尝试一下。
对于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);
}
现在,我将这个“修复”用于3和4。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.