此代碼用於處理以前版本的Android,因爲來自onActivityResult的意圖的數據將返回null,如果圖像來自相機,並且它將保存如果圖像是從文件系統中選擇的,則爲Uri。然而,在棒棒糖上,相機意圖也在返回一個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();
}
}
}
}
你的代碼是基於[這個答案](http://stackoverflow.com/questions/4455558/allow-用戶選擇相機或圖片庫的圖像/ 12347567#12347567),不是嗎? –
你的代碼決定不是基於Url,而是基於** data.getAction()**,所以我不知道爲什麼棒棒糖會使它與衆不同。 –
由於數據用於在圖像來自相機時返回null。沒有理由從空對象獲取數據。因此,如果指定「intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);」,則有關相機的老問題不會返回圖像。 –