0

Below is a snippet of a code I use to compress an image:

    public static final List<Object> compressImage(String imagePath) {
        Bitmap scaledBitmap = null;

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;

        Bitmap bmp = BitmapFactory.decodeFile( imagePath, options );

        int actualHeight = options.outWidth;
        int actualWidth  = options.outWidth;

        float maxHeight = 816.0f;
        float maxWidth = 612.0f;
        float imgRatio = actualWidth / actualHeight;
        float maxRatio = maxWidth / maxHeight;

        ................

        return List<Object>
    }

My problem is which options.outWitdth equals 0. I get error java.lang.ArithmeticException: divide by zero on line float imgRatio = actualWidth / actualHeight;

I've already seen this question: java.lang.ArithmeticException: divide by zero when compres image from pick galery

And tried to use the answer, but it did not work. I do not know what to try. How to fix it???

I get imagePath from Camera App:

private void dispatchTakePictureIntent() {
    // Create an intent to capture an image and returns control to the caller.
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileImageUri = ProcessaImagens.getOutputMediaFileUri(ProcessaImagens.MEDIA_TYPE_IMAGE, getApplicationContext());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileImageUri);
    // Starts the intent for image capture and wait for the result.
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

Class ProcessaImagens:

public static Uri getOutputMediaFileUri( int type, Context context ) {
    return Uri.fromFile( getOutputMediaFile( type, context ) );
}

private static File getOutputMediaFile( int type, Context context ) {
    // Obtem o nome do app para usar como o nome da pasta onde as imagens serao salvas dentro da pasta "Pictures"
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo = null;

    try {
        applicationInfo = packageManager.getApplicationInfo( context.getApplicationInfo().packageName, 0 );
    } catch ( final PackageManager.NameNotFoundException e ) {
    }
    String nomeApp = (String) (applicationInfo != null ? packageManager.getApplicationLabel( applicationInfo ) : "Desconhecido");

    if (nomeApp == null)
        nomeApp = context.getString(R.string.app_name);

    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    // Esta localizacao trabalha melhor se voce quer criar imagens para ser compartilhada entre aplicacoes e persistir depois de seu app ter sido desinstalado.
    File mediaStorageDir = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), nomeApp );

    // Cria o diretorio se ele nao existe
    if ( !mediaStorageDir.exists() ) {
        if ( !mediaStorageDir.mkdirs() ) {
            Log.d( nomeApp, "Falha ao criar diretório ou diretório já existe!" );
            return null;
        }
    }

    // Cria o nome do arquivo de midia
    String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss" ).format( new Date() );
    File mediaFile;
    if ( type == MEDIA_TYPE_IMAGE ) {
        mediaFile = new File( mediaStorageDir.getPath() + File.separator +
                "IMG_" + timeStamp + ".jpg" );
    } else if ( type == MEDIA_TYPE_VIDEO ) {
        mediaFile = new File( mediaStorageDir.getPath() + File.separator +
                "VID_" + timeStamp + ".mp4" );
    } else {
        return null;
    }

    return mediaFile;
}

Method onActivityResult where I call method to processImage:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // If finish Activity on startForActivityResult.
    if (resultCode == RESULT_OK) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            processImageCaptured();
        } else if (requestCode == SELECT_IMAGE_ACTIVITY_REQUEST_CODE) {
            fileImageUri = data.getData();
            new ProcessesImageSelectedTask().execute();
        }
    }
    // If cancel Activity on startForActivityResult.
    else if (resultCode == RESULT_CANCELED) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            // User cancel capture image.
        } else if (requestCode == SELECT_IMAGE_ACTIVITY_REQUEST_CODE) {}
    }
    // If an error occurred in the Activity on startForActivityResult.
    else {
        // Image capture fail, warning user.
        Toast.makeText(this, getString(R.string.fail_activity_take_image), Toast.LENGTH_SHORT).show();
    }
}

private void processImageCaptured() {
    galleryAddPic();
    List<Object> image = ProcessaImagens.compactarImagem(fileImageUri.getPath());

    .................
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(fileImageUri);
    this.sendBroadcast(mediaScanIntent);
}
Community
  • 1
  • 1
Lucas Santos
  • 597
  • 6
  • 18

2 Answers2

0

I think the problem is with the file Uri you are trying to create the image with. Are you sure your app has the WRITE_EXTERNAL_STORAGE permission? Because without it, the camera application cannot store your captured image.

Also, this may be a good place to start: https://developer.android.com/training/camera/photobasics.html

CodeMonkey
  • 41
  • 2
  • 8
  • Yeah, I sure my app have `WRITE_EXTERNAL_STORAGE` permission. – Lucas Santos Nov 16 '16 at 16:00
  • @LucasSantos I just tried your code, and it works just fine. – CodeMonkey Nov 16 '16 at 18:22
  • Can you provide some info on the device you are trying the code on? Some logs would be handy. Also the android version and hardware details – CodeMonkey Nov 16 '16 at 18:23
  • Samsung Galaxy S6, Android Marshmallow 6.0.1 @CodeMonkey – Lucas Santos Nov 16 '16 at 19:56
  • @LucasSantos Since it is android M, you may want to check the app permissions from the Application manager, because I tested on S6 with the same android version, and your code worked fine with the storage permission granted, but threw ArithmeticException when I went into the app settings and disabled the storage setting manually. – CodeMonkey Nov 17 '16 at 07:02
  • Strange. I check dangerous permissions runtime before execute app. I don't let use the App without permissions granted. – Lucas Santos Nov 17 '16 at 10:36
  • Thanks @CodeMonkey. I get resolve my problem with link your answer. The new documentation has changed since last time I saw. I follow the new documentation for take picture of camera and it's working. – Lucas Santos Nov 17 '16 at 11:11
-1

First, you need to select the image

public void onClick(View v) {//does whatever code is in here when the button is clicked
                    try {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
                    } catch (ActivityNotFoundException e){
                        Toast.makeText(Seller_Home_Page.this,"No application available to select image",Toast.LENGTH_SHORT).show();
                    }
                }

Then you need to grab the image data

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //For photo
    //so the if statement checks if the image came through, double checks if the result is okay and triple checks to see if the data is not null.
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        //File path to the photo that is selected.
        Uri ProductPhotoFilePath = data.getData();


        try {

            //Getting the Bitmap from Gallery
            productBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), ProductPhotoFilePath);

            ImagePath = getPath(ProductPhotoFilePath);

            persistProductImage(productBitmap, ImagePath);

            getStringImage(productBitmap);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(Seller_Home_Page.this,"Please pick a valid photo",Toast.LENGTH_SHORT).show();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

Then you compress the image

public void getStringImage(Bitmap bitmap) {

    if (bitmap != null){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 equals to the quality of the photo
        byte[] imageBytes = baos.toByteArray();

        int PhotoSize = imageBytes.length;

        if(PhotoSize <= 15000) {//Change this value to something more reasonable
            //Setting the Bitmap to ImageView
            ItemPhotoPreview.setImageBitmap(bitmap);

            encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        }else{
            //Setting the Bitmap to ImageView
            ItemPhotoPreview.setColorFilter(Color.TRANSPARENT);

            Toast.makeText(Seller_Home_Page.this,"Photo size is too large",Toast.LENGTH_SHORT).show();
        }
    }
}
Lazar Kukolj
  • 696
  • 3
  • 15
  • 43
  • I want select image from Camera App, i.e. an image captured by camera. I have a code which select image from Gallery App. And I compress code image is different of you. – Lucas Santos Nov 16 '16 at 14:10