我创建了一个选择器,用于从文件中选择图像或制作图片。
我使用的代码在Nexus 5上工作正常,但是当我在Samsung S5上试用时,选择器不会显示相机图标。
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;
}
当我更改将意图添加到选择器的顺序时,三星设备会显示相机,但只显示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相同的设备。但是我无法让它发挥作用。
答案 0 :(得分:8)
如果额外意图(Intent.EXTRA_INITIAL_INTENTS
)有多个意图匹配,则会在" Android系统" 下显示所有意图匹配。当您点击" Android系统"时,它将打开另一个具有所有这些意图匹配的选择器。
所以,在你为samsung设备拍摄的第一张截图中,相机确实出现了 - 它刚好在" Android Systeem" (单击此按钮将显示所有相机意图匹配)。
如果你使galleryIntent
更多而不是相机意图,它会将所有与画廊相关的意图组合在" Android Systeem" (如您在samsung设备的第二个屏幕截图中所示)
我想如果用户不知道这个" Android Systeem"是!
使用以下解决方案,您可以直接将每一个添加到chooser
在您的第二个代码段中,替换
Intent[] extraIntents = {galleryIntent};
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
with:
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 :(得分:2)
试试这种方式,希望这可以帮助您解决问题
请按照以下步骤操作,这是从图库和相机中选择图像的最佳和最简单的方法,
步骤1.声明全局变量
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.调用方法,在那里选择图片并显示对话框
picPhoto();
第4步。
在清单文件中设置用户权限
<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" />
由于
答案 2 :(得分:1)
尝试这种方式,我希望它适合你,我检查三星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);
}