用照相机意图和图像select器意图select器

我创build了一个从文件中挑选图像或制作图片的select器。

我使用的代码在Nexus 5上工作正常,但是当我在Samsung S5上试用时,select器不显示相机图标。

public Intent makePhotoIntent(String title, Context ctx, String uniqueImageId){ //Build galleryIntent Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryIntent.setType("image/*"); //Create chooser Intent chooser = Intent.createChooser(galleryIntent,title); if (checkexCameraHardware()){ Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); mTempImage = null; try { mTempImage = createImageFile(uniqueImageId); } catch (IOException e) { e.printStackTrace(); } if (mTempImage != null){ cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempImage)); //add file ure (photo is saved here) Intent[] extraIntents = {cameraIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); } } return chooser; } 

三星关系

当我更改意向添加到select器的顺序时,三星设备确实会显示相机,但只显示android-system作为文件选项。

 public Intent makePhotoIntent(String title, Context ctx, String uniqueImageId){ //Build galleryIntent Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryIntent.setType("image/*"); //Create chooser Intent chooser = Intent.createChooser(galleryIntent,title); if (checkexCameraHardware()){ Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); mTempImage = null; try { mTempImage = createImageFile(uniqueImageId); } catch (IOException e) { e.printStackTrace(); } if (mTempImage != null){ cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempImage)); //add file ure (photo is saved here) //I have to re-create the chooser here or the Samsung will not show the 'camera' icons. //I have to add the cameraIntent first. chooser = Intent.createChooser(cameraIntent,title); Intent[] extraIntents = {galleryIntent}; //Intent[] extraIntents = {cameraIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); } } return chooser; } 

三星关系

理想情况下,我希望三星设备显示相同的Nexus之一,当我首先添加画廊的意图。 我可以不能得到这个工作。

Solutions Collecting From Web of "用照相机意图和图像select器意图select器"

  • 如果有多个意图匹配( Intent.EXTRA_INITIAL_INTENTS ),则会在“Android系统”下显示所有这些匹配。 当你点击“Android系统”时,它将打开所有意向匹配的另一个select器。

  • 因此,在您的三星设备的第一个截图,相机确实显示 – 它只是在“Android系统”(点击此将显示所有相机意图匹配)。

  • 如果你使用galleryIntent代替摄影机意图,它将结合“Android系统”下的所有画廊相关的意图(如你的第二个截图中的三星设备)

我猜如果用户不知道这个“Android系统”是什么的话,用户可能会感到困惑!

  • 通过以下解决scheme,您可以直接将每一个添加到您的chooser

    在你的第二个代码片段中,replace

     Intent[] extraIntents = {galleryIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); 

    与:

     List<Intent> galleryIntents = new ArrayList<Intent>(); PackageManager pm = getApplicationContext().getPackageManager(); for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) { Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName); intent.setAction(Intent.ACTION_PICK); galleryIntents.add(intent); } chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, galleryIntents.toArray(new Parcelable[] {})); 

这将手动将每个匹配的意图直接添加到您的chooser

试试这样,希望这会帮助你解决你的问题

请按照这些步骤,这是从画廊和相机挑选图像的最佳和简单的方法,

步骤1.声明全局variables

 public static final int rcCC = 33; boolean isCC = false; 

第2步 。 复制这些方法并粘贴你的代码

 void picPhoto() { String str[] = new String[] { "Camera", "Gallery" }; new AlertDialog.Builder(getActivity()).setItems(str, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { performImgPicAction(which); } }).show(); } void performImgPicAction(int which) { Intent in; if (which == 1) { in = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); } else { in = new Intent(); in.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } startActivityForResult( Intent.createChooser(in, "Select profile picture"), which); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if (rcCC == requestCode) { getActivity(); if (resultCode == Activity.RESULT_OK) { isCC = true; } } else { getActivity(); if (resultCode == Activity.RESULT_OK) { BitmapFactory.Options option = new BitmapFactory.Options(); option.inDither = false; option.inPurgeable = true; option.inInputShareable = true; option.inTempStorage = new byte[32 * 1024]; option.inPreferredConfig = Bitmap.Config.RGB_565; if (Build.VERSION.SDK_INT < 19) { Uri selectedImageURI = data.getData(); uImage = BitmapFactory.decodeFile(Common.getPath( selectedImageURI, getActivity()), option); } else { ParcelFileDescriptor pfd; try { pfd = context .getContentResolver() .openFileDescriptor(data.getData(), "r"); FileDescriptor fileDescriptor = pfd .getFileDescriptor(); uImage = BitmapFactory.decodeFileDescriptor( fileDescriptor, null, option); pfd.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } try { ivProfile_image.setImageBitmap(uImage); } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); } } 

第3步。调用方法,你要select图片和显示对话框

  picPhoto(); 

步骤4。

在清单文件中build立用户权限,

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> 

谢谢

试试这样,我希望它的作品为你,我检查三星Galaxy Tab 2它显示相机选项

宣布你的意图是这样的

  Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryIntent.setType("image/*"); //Create chooser Intent chooser = Intent.createChooser(galleryIntent,title); Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(uniqueImageId); cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri); //add file ure (photo is saved here) Intent[] extraIntents = {cameraIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); startActivityForResult(chooser, Constants.SEND_IMAGE_FROM_CEMERA); 

和getOutputMediaFileUri(uniqueImageId)方法,这将返回你的图像的Uri

 getOutputMediaFileUri(String uniqueImageId){ File mediaStorageDir = new File( Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "IMAGE_DIRECTORY_NAME"); // Create the storage directory if it does not exist if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { return null; } } File mediaFile; mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + uniqueImageId + ".jpg"); return Uri.fromFile(mediaFile); }