Android - ACTION_IMAGE_CAPTURE, Problem to capture image

Asked

Viewed 405 times

1

I’m developing an application that needs a photo of the user. For this, I 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 feature, 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 on onActivityResult:

if (requestCode == CAMERA_REQUEST_PERFIL) {
            SharedPreferencesManager.setCallbexEditado(this, true);
            if (cameraControler != null) {
                fragEditarDados.atualizaImagemPerfil(cameraControler
                        .getImagemCamera());
            }
        }

I debug the devices: 1 - Galaxy Gran Prime Duos - 4.4.4 2 - Motorcycle 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 one, I can capture the image, but I can’t cut it. In device 3 the native camera of the device breaks. In devices 2 and 4 when returning to Activity, in onActivityResult, the global variables, which before the opening of the camera were all instantiated and valued, are all null, which makes it impossible to get the image.

Someone has been through something like this and could help me?

  • What version of each one’s android? What is the max min version of your app, in the manifest? What version of your SDK?

  • My SDK is last available version. compileSdkVersion 21 buildToolsVersion "21.1.2" minSdkVersion 10 targetSdkVersion 21

  • What the launchMode of that Activity no AndroidManifest?

  • Is the default "standard"

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.