此代码用于以前版本的 Android,因为来自 onActivityResult 中的 Intent 的数据如果图像来自相机,将返回 null,如果图像是从文件系统中选择的,它将保存一个 Uri。然而,在 Lollipop 上,相机 Intent 也返回一个 Uri。我如何知道图像是用相机拍摄的还是从文件存储/图库中选择的?
这是我的代码:
private void openImageIntent() {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Klea" + File.separator);
root.mkdirs();
final String fname = getUniqueImageFilename();
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getActivity().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,
getString(R.string.chose_source));
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE);
}
private static String getUniqueImageFilename() {
// TODO Auto-generated method stub
String fileName = "img_" + System.currentTimeMillis() + ".jpg";
return fileName;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("reqCode", Integer.toString(requestCode));
Log.d("resCode", Integer.toString(resultCode));
Log.d("data", data.toString());
getActivity();
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action
.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
File file = new File(outputFileUri.toString());
Log.d("old file", file.toString());
String temp = file.toString().replace("file:", "");
Log.d("new file", temp);
selectedImageUri = Uri.parse(temp);
} else {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
selectedImageUri = Uri.parse(cursor.getString(columnIndex));
cursor.close();
}
}
}
}
最佳答案
首先,无法为您提供任何保证。用户可以选择在其 list 中声明 MediaStore.ACTION_IMAGE_CAPTURE Intent 的任何应用,并且该应用可以决定从文件系统中选择图片。
其次,看看新的createChooser()采用 IntentSender 的方法参数返回最终用户的选择供您分析,参见this example (不幸的是,这是 22 岁及以上)。
第三,您提供 outputFileUri 作为 MediaStore.EXTRA_OUTPUT - 该文件有望由相机创建,但会被文件系统选择器忽略;现在,如果您只在调用启动 Intent 之前 进行清理,就可以轻松检查文件是否包含新图片。
我同意你的conclusion基于data.dat (content://media/...
-> file, file:/...
-> camera) 的选择不是足够可靠。
所以最重要的是,可以像这样进行简单但可靠的区分:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
final boolean isCamera = new File(outputFileUri.getPath()).length() > 0;
…
关于android - 如何知道图像是来自相机还是文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33649479/