gpt4 book ai didi

android - 如何避免在相机 Intent 上使用 MediaStore.EXTRA_OUTPUT?

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:44 27 4
gpt4 key购买 nike

我正在尝试让我的用户从手机图库中选择一张照片或使用相机拍照。我写了一段代码,在我的三星 S6 上运行得非常好

private void openPictureIntent() {
final List<Intent> cameraIntents = new ArrayList<>();
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);
cameraIntents.add(intent);
}

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

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

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

startActivityForResult(chooserIntent, SELECT_PICTURE_CODE);
}

然后我在 Nexus 5X 上试了一下,我的应用程序崩溃了。我了解到在 onActivityResult 中检索到的 Intent 对于某些设备为 null。解决方案是给 uri 在哪里保存用相机拍摄的图像。我实现了它,我的代码变成了

private void openPictureIntent() {
// Determine Uri of camera image to save.
final File dir = new File(Environment.getExternalStorageDirectory() + File.separator + "Directory" + File.separator);
dir.mkdirs();
final String fname = UUID.randomUUID() + ".jpg";
final File sdImageMainDirectory = new File(dir, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
new File(outputFileUri.getPath()).delete();

// Camera.
final List<Intent> cameraIntents = new ArrayList<>();
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);
}

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

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

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

startActivityForResult(chooserIntent, SELECT_PICTURE_CODE);
}

这是可行的,但我不满意。我希望我的照片与其他相机照片一起存储,而不是在特殊目录中。我怎样才能做到这一点?

最佳答案

I learned that the intent retrieved in onActivityResult is null with some devices.

对于所有相机应用程序,它应该是 null。一些相机应用程序开发人员无法阅读 the documentation for ACTION_IMAGE_CAPTURE .

I would like my photo to be stored with other camera photo and not in a special directory

ACTION_IMAGE_CAPTURE 不支持。

关于android - 如何避免在相机 Intent 上使用 MediaStore.EXTRA_OUTPUT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43368794/

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