gpt4 book ai didi

android - 如何使用一个 Intent 访问android中的照片和相机

转载 作者:行者123 更新时间:2023-11-30 01:42:28 24 4
gpt4 key购买 nike

如果您使用 whatsapp 并单击附加按钮,将展开一个菜单/弹出窗口,显示您可以用来选择图像甚至使用照片应用从相机拍照

whatapp popup

我制作了一个应用程序,其功能与访问和拍照的 whatapp 几乎相同。问题是,我无法使用在我的弹出对话框中列为应用程序之一的照片应用拍照,但我可以在使用 whatsapp 时拍照。当我从我显示的对话框中单击照片应用时应用程序,它会显示我所有的照片。但是当我从 whatsapp 弹出窗口中点击照片应用时,它直接进入相机

public class ImagePickerActivity extends Activity {

private final int SELECT_PHOTO = 1;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_picker);

imageView = (ImageView)findViewById(R.id.imageView);

Button pickImage = (Button) findViewById(R.id.btn_pick);
pickImage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
try {
final Uri imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}
}
}

在我的 android list 中

<uses-feature android:name="android.hardware.camera"
android:required="true" />

我需要更改代码中的哪些内容才能从照片应用中拍照?

最佳答案

下面的代码将在 native 对话框中显示相机和浏览存储选项。

 // Picks Camera first.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}

final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);

// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
"Select Image from");

// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[]{}));

startActivityForResult(chooserIntent, TAKE_PHOTO_CODE);

这对你有帮助!

并在自定义对话框中显示与 whatsapp 相同的内容,请引用下面的链接 click here ..

关于android - 如何使用一个 Intent 访问android中的照片和相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34269560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com