cropIntent does not work on Motorola - editing not compatible with image type

Asked

Viewed 39 times

0

In the onCreate

    mBotFoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            try {
                mFileTemp = new File(getActivity().getCacheDir(), "temp_photo2.jpg");
                uri = FileProvider.getUriForFile(getContext(), "br.com.legalestudioapp.legalmototaxiagente.fileprovider2", mFileTemp);
                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
                intent.putExtra("return-data", true);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivityForResult(intent, IMAGEM_CAMERA);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(getContext(), "Não foi possível abrir a câmera!", Toast.LENGTH_LONG).show();
            }
        }
    });


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == IMAGEM_CAMERA) {
            if (mFileTemp == null) {
                Toast.makeText(getContext(), "nao carregou foto da camera", Toast.LENGTH_LONG).show();
                return;
            }

            //Uri uri = FileProvider.getUriForFile(getActivity().getApplicationContext(), "br.com.legalestudioapp.legalmototaxiagente.fileprovider", mFileTemp);
            // Intent i=new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, mFileTemp));
            try {
                Intent cropIntent = new Intent("com.android.camera.action.CROP");
                cropIntent.setDataAndType(uri, "image/*");
                cropIntent.putExtra("crop", "true");
                cropIntent.putExtra("aspectX", 1);
                cropIntent.putExtra("aspectY", 1);
                cropIntent.putExtra("outputX", 640);
                cropIntent.putExtra("outputY", 640);
                cropIntent.putExtra("return-data", true);
                cropIntent.putExtra("noFaceDetection", true);
                cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivityForResult(cropIntent, PIC_CROP);
            } catch (Exception e) {
                Toast.makeText(getContext(), "não foi possível editar", Toast.LENGTH_LONG).show();
                Bundle extras = data.getExtras();
                //get the cropped bitmap
                Bitmap foto = extras.getParcelable("data");
                mFotoC.setImageBitmap(foto);
                mFoto = foto;
                Long tsLong = System.currentTimeMillis() / 1000;
                timestamp = tsLong.toString();
            }


        }
        if (requestCode == PIC_CROP) {
            Bundle extras = data.getExtras();
            //get the cropped bitmap
            Bitmap foto = extras.getParcelable("data");
            mFotoC.setImageBitmap(foto);
            mFoto = foto;
            Long tsLong = System.currentTimeMillis() / 1000;
            timestamp = tsLong.toString();
        }
    }
}
  • Welcome to the Stackoverflow in Portuguese. Do the [tour] to learn how the community, access [help] to get help on the site tools and to make good use of it.

  • remembering that it appears Toast message, saying that it cannot edit the image because it is incompatible, it no longer executes the catch code...

1 answer

0


well people I even figured out how to circumvent the problem, is not the best solution, because there is no cutout screen for the user, but finally the code was like this:

mMarcaCell = Build.MANUFACTURER;

mBotFotoNova.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (mMarcaCell.equals("motorola")) {
                    startActivityForResult(intent, IMAGEM_CAMERA);
                } else {
                    try {
                        mFileTemp = new File(getFilesDir(), "temp_photo.jpg");
                        Uri uri = FileProvider.getUriForFile(getApplicationContext(), "br.com.legalestudioapp.legalmototaxi.fileprovider", mFileTemp);
                        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
                        intent.putExtra("return-data", true);
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        startActivityForResult(intent, IMAGEM_CAMERA);
                    } catch (ActivityNotFoundException e) {
                        Toast.makeText(getApplicationContext(), "Não foi possivel abrir a câmera!", Toast.LENGTH_LONG).show();
                    }
                }
            }
        });

final int PIC_CROP = 3;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == IMAGEM_GALERIA) {
                Uri imagemUri = data.getData();
                if (mMarcaCell.equals("motorola")) {
                    final InputStream imageStream;
                    try {
                        imageStream = getContentResolver().openInputStream(imagemUri);
                        Bitmap foto = BitmapFactory.decodeStream(imageStream);
                        foto = scaleCenterCrop(foto, 640,640);
                        mFotoPass.setImageBitmap(foto);
                        mFoto = foto;
                        Long tsLong = System.currentTimeMillis() / 1000;
                        timestamp = tsLong.toString();
                    } catch (FileNotFoundException e) {
                        Toast.makeText(this, "Não foi possível selecionar esta foto!", Toast.LENGTH_LONG).show();
                    }
                } else {
                    try {
                        Intent cropIntent = new Intent("com.android.camera.action.CROP");
                        cropIntent.setDataAndType(imagemUri, "image/*");
                        cropIntent.putExtra("crop", "true");
                        cropIntent.putExtra("aspectX", 1);
                        cropIntent.putExtra("aspectY", 1);
                        cropIntent.putExtra("outputX", 640);
                        cropIntent.putExtra("outputY", 640);
                        cropIntent.putExtra("return-data", true);
                        startActivityForResult(cropIntent, PIC_CROP);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(this, "Não foi possivel recortar a foto!", Toast.LENGTH_LONG).show();
                    }
                }
            }

            if (requestCode == IMAGEM_CAMERA) {
                if (mMarcaCell.equals("motorola")) {
                    Bundle extras = data.getExtras();
                    //get the cropped bitmap
                    Bitmap foto = extras.getParcelable("data");
                    foto = scaleCenterCrop(foto, 640,640);
                    mFotoPass.setImageBitmap(foto);
                    mFoto = foto;
                    Long tsLong = System.currentTimeMillis() / 1000;
                    timestamp = tsLong.toString();
                }else {
                    if (mFileTemp == null) {
                        Toast.makeText(this, "nao carregou foto da camera", Toast.LENGTH_LONG).show();
                        return;
                    }

                    Uri uri = FileProvider.getUriForFile(getApplicationContext(), "br.com.legalestudioapp.legalmototaxi.fileprovider", mFileTemp);
                    // Intent i=new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, mFileTemp));
                    Intent cropIntent = new Intent("com.android.camera.action.CROP");
                    cropIntent.setDataAndType(uri, "image/*");
                    cropIntent.putExtra("crop", "true");
                    cropIntent.putExtra("aspectX", 1);
                    cropIntent.putExtra("aspectY", 1);
                    cropIntent.putExtra("outputX", 640);
                    cropIntent.putExtra("outputY", 640);
                    cropIntent.putExtra("return-data", true);
                    cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivityForResult(cropIntent, PIC_CROP);
                }
            }

            if (requestCode == PIC_CROP) {
                Bundle extras = data.getExtras();
                //get the cropped bitmap
                Bitmap foto = extras.getParcelable("data");
                mFotoPass.setImageBitmap(foto);
                //Bitmap resized = Bitmap.createScaledBitmap(foto, 640, 640, true);
                mFoto = foto;
                Long tsLong = System.currentTimeMillis() / 1000;
                timestamp = tsLong.toString();
            }
        }
    }

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
        int sourceWidth = source.getWidth();
        int sourceHeight = source.getHeight();

        // Compute the scaling factors to fit the new height and width, respectively.
        // To cover the final image, the final scaling will be the bigger
        // of these two.
        float xScale = (float) newWidth / sourceWidth;
        float yScale = (float) newHeight / sourceHeight;
        float scale = Math.max(xScale, yScale);

        // Now get the size of the source bitmap when scaled
        float scaledWidth = scale * sourceWidth;
        float scaledHeight = scale * sourceHeight;

        // Let's find out the upper left coordinates if the scaled bitmap
        // should be centered in the new size give by the parameters
        float left = (newWidth - scaledWidth) / 2;
        float top = (newHeight - scaledHeight) / 2;

        // The target rectangle for the new, scaled version of the source bitmap will now
        // be
        RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

        // Finally, we create a new bitmap of the specified size and draw our new,
        // scaled bitmap onto it.
        Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
        Canvas canvas = new Canvas(dest);
        canvas.drawBitmap(source, null, targetRect, null);

        return dest;
    }

Browser other questions tagged

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