临时相机调用页面
/**
* 临时照相页面
*/
public class CameraTempActivity extends JCBBaseActivity {
public static final String USER_ID = "user_id";
public static final String SESSION_TYPE = "session_type";
public void start(Context context, String userId, SessionTypeEnum sessiongType){
Intent it = new Intent();
it.setClass(context,CameraTempActivity.class);
it.putExtra(USER_ID,userId);
it.putExtra(SESSION_TYPE,sessiongType);
startActivity(it);
}
String outPath = UStorage.getWritePath(HString.get32UUID() + ".jpg", StorageType.TYPE_TEMP);
private static final int REQUEST_CODE_CAMERA = 2;
@Override
protected void initView(Bundle savedInstanceState) {
super.initView(savedInstanceState);
try {
if (TextUtils.isEmpty(outPath)) {
toast(getResources().getString(R.string.sdcard_not_enough_error), Toast.LENGTH_LONG);
finish();
return;
}
File outputFile = new File(outPath);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outputFile));
startActivityForResult(intent, REQUEST_CODE_CAMERA);
} catch (ActivityNotFoundException e) {
finish();
} catch (Exception e) {
toast(getResources().getString(R.string.sdcard_not_enough_head_error), Toast.LENGTH_LONG);
finish();
}
}
@Override
protected void startView(Bundle savedInstanceState) {
}
@Override
protected int layoutViewId() {
return R.layout.activity_camera_temp;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
setResult(resultCode);
finish();
return;
}
onPickedCamera(data, requestCode);
}
private void onPickedCamera(Intent data, int code) {
try {
String photoPath = pathFromResult(data);
if (!TextUtils.isEmpty(photoPath)) {
Intent it = getIntent();
String userId = it.getStringExtra(USER_ID);
SessionTypeEnum sessionType = (SessionTypeEnum) it.getSerializableExtra(SESSION_TYPE);
CameraActivity.start(CameraTempActivity.this,userId,sessionType,photoPath);
}
finish();
} catch (Exception e) {
toast(getResources().getString(R.string.picker_image_error), Toast.LENGTH_LONG);
finish();
}
}
private String pathFromResult(Intent data) {
if (data == null || data.getData() == null) {
return outPath;
}
Uri uri = data.getData();
Cursor cursor = getContentResolver()
.query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
if (cursor == null) {
// miui 2.3 有可能为null
return uri.getPath();
} else {
if (uri.toString().contains("content://com.android.providers.media.documents/document/image")) { // htc 某些手机
// 获取图片地址
String _id = null;
String uridecode = uri.decode(uri.toString());
int id_index = uridecode.lastIndexOf(":");
_id = uridecode.substring(id_index + 1);
Cursor mcursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, " _id = " + _id,
null, null);
mcursor.moveToFirst();
int column_index = mcursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
outPath = mcursor.getString(column_index);
if (!mcursor.isClosed()) {
mcursor.close();
}
if (!cursor.isClosed()) {
cursor.close();
}
return outPath;
} else {
cursor.moveToFirst();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
outPath = cursor.getString(column_index);
if (!cursor.isClosed()) {
cursor.close();
}
return outPath;
}
}
}
}