我从这段代码中调用相机意图:
void opencamera(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = Common.getOutputMediaFileUri(Common.MEDIA_TYPE_IMAGE,
CameraActivity.this);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent,
Common.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
private static File getOutputMediaFile(int type, Context context) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir;
File mediaStorageProfileDir;
if (isSdPresent() == true) {
mediaStorageDir = new File(Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getPath(),
"test/XYZ");
mediaStorageProfileDir = new File(Environment
.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getPath(),
"test/XYZ");
} else {
mediaStorageDir = new File(context.getCacheDir(),
"test/XYZ");
mediaStorageProfileDir = new File(context.getCacheDir(),
"test/XYZ");
}
if (!mediaStorageDir.exists()) {
mediaStorageDir.getParentFile().mkdirs();
} else if (mediaStorageDir.exists()) {
}
if (!mediaStorageProfileDir.exists()) {
mediaStorageProfileDir.getParentFile().mkdirs();
} else if (mediaStorageProfileDir.exists()) {
}
boolean setWritable, setwritable = false;
setWritable = mediaStorageDir.setWritable(true, false);
setwritable = mediaStorageProfileDir.setWritable(true, false);
Toast.makeText(
context,
"mediaStorageDir ::" + setWritable
+ " mediaStorageProfileDir :: " + setwritable,
Toast.LENGTH_SHORT).show();
String timeStamp = new SimpleDateFormat("MMM dd,yyyy HH:mm a")
.format(new Date());
File mediaFile = null;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getAbsolutePath()
+ File.separator + "test" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
此代码适用于 Moto E、Moto G、Samsung Duos 设备,但此代码不适用于 Google Nexus s 4g(版本 4.1.0),在 Nexus s 中它打开相机但拍照后不允许我点击打勾,因为 OnActivityResult 没有被调用。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(this, "onActivityResult fire", Toast.LENGTH_SHORT)
.show();
if (requestCode == Common.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
try {
Toast.makeText(
this,
"result code : " + resultCode + " data :: " + data
+ "request code ::" + requestCode,
Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK) {
Toast.makeText(this,
"onActivity result file URI :::" + fileUri,
Toast.LENGTH_SHORT).show();
if (fileUri != null) {
this.getContentResolver().notifyChange(fileUri, null);
ContentResolver cr = this.getContentResolver();
try {
// create high quality bitmap
ExifInterface exif = new ExifInterface(
fileUri.toString());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, fileUri);
// rotate bitmap.
bitmap = rotateBitmap(bitmap, orientation);
imgUpload.setImageBitmap(bitmap);
// RLcamera.setVisibility(View.GONE);
if (bitmap != null) {
img_categoryedit.setVisibility(View.VISIBLE);
} else {
img_categoryedit.setVisibility(View.GONE);
}
if (bitmap != null) {
ShowWindowPopUp();
Visible_InVisible(true);
}
} catch (Exception e) {
Toast.makeText(this, "Failed to load",
Toast.LENGTH_SHORT).show();
}
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "resultCode cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Image capture failed, advise user
Toast.makeText(this, "Image capture failed.",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
}
}
}