I have problem about capturing image on Android via camera. I show my source of my Android app below.
public class RegisterActivity extends AppCompatActivity {
private Kelolajson json;
private Button btnfoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
......
json = new KelolaJson();
btnfoto = (Button) findViewById(R.id.fotobtn);
btnfoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isDeviceSupportCamera()) {
f_foto =txthp.getText().toString() +".jpg";
fname= txthp.getText().toString();
captureImage();
}
else {
Toast.makeText(getApplicationContext(),"Your Mobile don't support camera",Toast.LENGTH_LONG).show();
}
}
});
......
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image display it in image view
String root = Environment.getExternalStorageDirectory().toString();
String path = root + "/Pictures/myapp";
gmbpath = fileUri.getPath().toString();
Bitmap tmp = BitmapFactory.decodeFile(gmbpath);
imgpreview.setImageBitmap(tmp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"You has been canceled capture.",
Toast.LENGTH_SHORT).show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry, Can\'t get photo",
Toast.LENGTH_SHORT).show();
}
}
}
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type) {
//External sdcard location
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"myapp");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile = null;
if (type == MEDIA_TYPE_IMAGE) {
fname = "_" + timeStamp;
f_foto = tmp_hp + ".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + f_foto);
} else {
mediaFile= null;
}
return mediaFile;
}
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
}
This source code has been running well on mobile Smartphone. But some smartphone's result of captured photo always changed orientation. For example, I capture image by smartphone portrait orientation but result of smartphone capture image is landscape orientation. Also, if landscape orientation, it is result changed to portrait orientation. This result don't follow smartphone orientation.