如何知道图像是来自相机还是文件系统
How to know if image is from camera or file system
此代码用于以前版本的 Android,因为如果图像来自相机,来自 onActivityResult 中的意图的数据会 return null,并且它会包含一个 Uri如果图像是从文件系统中选择的。但是,在 Lollipop 上,相机意图也是 returning 一个 Uri。我如何知道图像是用相机拍摄的还是从文件中选择的storage/gallery?
这是我的代码:
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();
}
}
}
}
首先,无法为您提供任何保证。用户可以选择在其清单中声明 MediaStore.ACTION_IMAGE_CAPTURE 意图的任何应用,并且该应用可以决定从文件系统中选择图片。
其次,看看新的 createChooser() method that takes an IntentSender parameter to return the end user's choice for you to analyze, see (不幸的是,这是 22 岁及以上)。
第三,您提供 outputFileUri 作为 MediaStore.EXTRA_OUTPUT - 这个文件有望由相机创建,但会被忽略文件系统选择器;现在,如果您仅在 调用启动 Intent 之前清理,您可以轻松检查文件是否包含新图片。
我同意你的 选择基于 data.dat (content://media/…
-> 文件, file:/…
->相机)不够可靠。
所以最重要的是,可以像这样进行简单但稳健的区分:
@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,因为如果图像来自相机,来自 onActivityResult 中的意图的数据会 return null,并且它会包含一个 Uri如果图像是从文件系统中选择的。但是,在 Lollipop 上,相机意图也是 returning 一个 Uri。我如何知道图像是用相机拍摄的还是从文件中选择的storage/gallery?
这是我的代码:
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();
}
}
}
}
首先,无法为您提供任何保证。用户可以选择在其清单中声明 MediaStore.ACTION_IMAGE_CAPTURE 意图的任何应用,并且该应用可以决定从文件系统中选择图片。
其次,看看新的 createChooser() method that takes an IntentSender parameter to return the end user's choice for you to analyze, see
第三,您提供 outputFileUri 作为 MediaStore.EXTRA_OUTPUT - 这个文件有望由相机创建,但会被忽略文件系统选择器;现在,如果您仅在 调用启动 Intent 之前清理,您可以轻松检查文件是否包含新图片。
我同意你的 content://media/…
-> 文件, file:/…
->相机)不够可靠。
所以最重要的是,可以像这样进行简单但稳健的区分:
@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;
…