I'm developing an application that needs a photo of the user. For this, I have developed a code that is responsible for capturing and resizing the image. This is code:
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
String dirPath = Environment.getExternalStorageDirectory()
+ "/MyApplication/imagens/";
File projDir = new File(dirPath);
if (!projDir.exists()) {
projDir.mkdirs();
}
File image = File.createTempFile(imageFileName, ".jpg", projDir);
currentPhotoPath = image.getAbsolutePath();
return image;
}
public void abrirCamera(Activity activity) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagem = null;
try {
imagem = createImageFile();
if (imagem != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagem));
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", cameraConfig.getLargura());
intent.putExtra("outputY", cameraConfig.getAltura());
activity.startActivityForResult(intent,
cameraConfig.getResultCodeCamera());
}
} catch (IOException ex) {
}
}
public Bitmap getImagemCamera() {
Bitmap bitmap = null;
if (currentPhotoPath != null) {
ImageView imagemCamera = cameraConfig.getImageView();
int targetW = imagemCamera.getWidth();
int targetH = imagemCamera.getHeight();
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
deletarArquivo(currentPhotoPath);
}
return bitmap;
}
To make use of this functionality, in my activity I have this code:
public void abrirCamera(ImageView imagemPerfil) {
//cameraControler e conf são variáveis globais
if (cameraControler == null) {
conf = new CameraConfig(imagemPerfil, CAMERA_REQUEST_PERFIL,
CAMERA_REQUEST_GALERIA, 512, 512);
cameraControler = new CameraController(conf);
}
cameraControler.abrirCamera(this);
}
and no onActivityResult:
if (requestCode == CAMERA_REQUEST_PERFIL) {
SharedPreferencesManager.setCallbexEditado(this, true);
if (cameraControler != null) {
fragEditarDados.atualizaImagemPerfil(cameraControler
.getImagemCamera());
}
}
I debug the devices: 1 - Galaxy Grand Prime Duos - 4.4.4 2 - Moto G - 4.4.4 3 - Zenfone 5 - 4.4.2 4 - Galaxy tab 3 - 4.1.2
My problem is subdivided into the following points: On device 1 I can capture the image, but I can not cut it. In unit 3 the camera's native device breaks down. In devices 2 and 4 when returning to activity, in the onActivityResult, the global variables, which were all instantiated and valued before the camera were opened, are all null, which makes it impossible to get the image.
Someone has gone through something like this and could help me?