I made an android app that contains : (1) A button 'capture photo' which when clicked opens the device camera. (2) An ImageView to show the full sized image captured from the camera.
What I did was save the full sized image and then load the same onto the ImageView. This works as intended in an android device running version 5.1.1. However, in a device with android version 4.1.1 and another device with version 7.0, it crashes when the 'CAPTURE PHOTO' button is pressed. I couldn't find the reason. Here's my java code for the same and thanks in advance :
public class MainActivity extends AppCompatActivity {
private ImageView imageHolder;
private final int requestCode = 20;
private File mTempCameraPhotoFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageHolder = (ImageView)findViewById(R.id.captured_photo);
Button capturedImageButton = (Button)findViewById(R.id.photo_button);
capturedImageButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File exportDir = new
File(Environment.getExternalStorageDirectory(), "TempFolder");
if (!exportDir.exists()) {
exportDir.mkdirs();
} else {
exportDir.delete();
}
mTempCameraPhotoFile = new File(exportDir, "/" +
UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");
// Log.d(LOG_TAG, "/" +
UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(mTempCameraPhotoFile));
startActivityForResult(takePictureIntent, requestCode);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK){
String filePath = mTempCameraPhotoFile.getPath();
Drawable img = Drawable.createFromPath(filePath);
imageHolder.setImageDrawable(img);
Toast.makeText(this, "Image path"+filePath,
Toast.LENGTH_LONG).show();
}
}
}