Réduire la taille d'une image à une taille spécifiée dans Android

Je veux réduire la taille d'une image bitmap à 200kb exactement. J'obtiens une image de la carte sd, compresser et enregistrer sur la carte sd de nouveau avec un autre nom dans un autre répertoire. La Compression fonctionne très bien (3 mo comme l'image est compressée à environ 100 ko). J'ai écrit ces lignes de codes pour ce:

String imagefile ="/sdcard/DCIM/100ANDRO/DSC_0530.jpg";
Bitmap bm = ShrinkBitmap(imagefile, 300, 300);

//this method compresses the image and saves into a location in sdcard
    Bitmap ShrinkBitmap(String file, int width, int height){

         BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

            if (heightRatio > 1 || widthRatio > 1)
            {
             if (heightRatio > widthRatio)
             {
              bmpFactoryOptions.inSampleSize = heightRatio;
             } else {
              bmpFactoryOptions.inSampleSize = widthRatio; 
             }
            }

            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();   
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
            byte[] imageInByte = stream.toByteArray(); 
            //this gives the size of the compressed image in kb
            long lengthbmp = imageInByte.length / 1024; 

            try {
                bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/compressed_new.jpg"));
            } catch (FileNotFoundException e) {
                //TODO Auto-generated catch block
                e.printStackTrace();
            }


         return bitmap;
        }
modifier la largeur et la hauteur de cette image..
tu veux dire pour rendre l'image 200x200?
ouais, comme vous voulez 200ko.. essayer jusqu'à ce que vous obtenez votre résultat..
ok, si je donne une largeur constante et une hauteur, les images de la même taille toujours?, J'ai déjà fait: ShrinkBitmap(imagefile, 300, 300)
non, changer la largeur et la hauteur en fonction de votre taille.. si vous obtenez 100 ko sur 300*300 puis pour faire la taille de 200ko, à accroître à la fois la largeur et la hauteur..

OriginalL'auteur TharakaNirmana | 2013-06-06