I am new on Android. I am testing an Android application (built after searching on internet) that creates an Intent in the main activity that is started onClick of a button, starts the Camera of the device and returns the photo taken to an ImageView in the main layout. The problem I have is that while testing on the emulator the process finishes successfully (the picture shows up on the ImageView after saving the photo), the same happens while testing on the Samsung Tablet 2 (GT-P7510) with Android version 4.0.4, but when I try to run it on the Samsung S4 smartphone (GT-I9500) with version 5.0.1 it saves the picture but the photo is not shown in ImageView on main layout.
Does someone had the same issue?
I read it could be to some problem on the OnCreate but couldn't solve it.
Here's part of the code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_camera);
photoImage = (ImageView) findViewById(R.id.photo_image);
callCameraButton = (Button)
findViewById(R.id.button_callcamera);
photoImage.setVisibility(View.VISIBLE);
callCameraButton.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = Uri.fromFile(getOutputPhotoFile());
i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
archivo = fileUri.getEncodedPath();
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
if (resultCode == RESULT_OK) {
Uri photoUri = null;
if (data == null) {
// A known bug here! The image should have saved in fileUri
Toast.makeText(this, "Image saved successfully",
Toast.LENGTH_LONG).show();
photoUri = fileUri;
} else {
photoUri = data.getData();
Toast.makeText(this, "Image saved successfully in: " + data.getData(),
Toast.LENGTH_LONG).show();
}
showPhoto(archivo);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Callout for image capture failed!",
Toast.LENGTH_LONG).show();
}
}
}
private void showPhoto(String photoUri) {
File imageFile = new File (photoUri);
if (imageFile.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
photoImage.setImageDrawable(drawable);
}
}