@RKN
Votre méthode peut aussi lancer OutOfMemoryError exception - si bitmap recadrée dépasse VM.
Ma méthode combine vôtre et la protection contre cette exeption: (l, t, r, b -% de l'image)
Bitmap cropBitmap(ContentResolver cr, String file, float l, float t, float r, float b)
{
try
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.decodeFile(file, options);
int oWidth = options.outWidth;
int oHeight = options.outHeight;
InputStream istream = cr.openInputStream(Uri.fromFile(new File(file)));
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(istream, false);
if (decoder != null)
{
options = new BitmapFactory.Options();
int startingSize = 1;
if ((r - l) * oWidth * (b - t) * oHeight > 2073600)
startingSize = (int) ((r - l) * oWidth * (b - t) * oHeight/2073600) + 1;
for (options.inSampleSize = startingSize; options.inSampleSize <= 32; options.inSampleSize++)
{
try
{
return decoder.decodeRegion(new Rect((int) (l * oWidth), (int) (t * oHeight), (int) (r * oWidth), (int) (b * oHeight)), options);
}
catch (OutOfMemoryError e)
{
Continue with for loop if OutOfMemoryError occurs
}
}
}
else
return null;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
et retourne bitmap disponible max ou null
Merci Steve, mais cela me obligerait à charger dans le sourceBitmap premier, et je suis en train d'éviter de faire cela, car il est trop grand pour tenir dans la mémoire, et je veux travailler avec un bitmap qui n'a pas été sous-échantillonnée . – Schermvlieger
Oups, vous avez raison, je n'ai pas lu cette partie de votre question correctement. Je ne suis pas sûr alors comment faire ceci sans downsampling. En fait, un problème très similaire est aussi sur ma propre liste figure-out-to-do! –