【发布时间】:2017-04-12 11:53:24
【问题描述】:
我试图让我的用户从手机图库中挑选一张照片或用相机拍照。 我写了一段在我的三星 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 中检索到的意图对于某些设备是空的。解决方案是为 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);
}
这是有效的,但我不满意。我希望我的照片与其他相机照片一起存储,而不是在特殊目录中。我怎样才能做到这一点?
【问题讨论】:
-
保存图片 Environment.DIRECTORY_PICTURES 目录
-
画廊意图不会在我的设备上显示图片目录中的照片(DCIM 存储库也是如此),我希望在每个设备上都有相同的结果
-
存储在
Environment.DIRECTORY_PICTURES和Environment.DIRECTORY_DCIM中的图像完全符合MediaStore的扫描条件,因此会显示在图库类型的应用程序中。它们最终将被自动编入索引,开发人员可以使用MediaScannerConnection在创建特定文件时将它们添加到MediaStore。
标签: android android-intent android-camera