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);
}