@RKN
Su método también puede lanzar una excepción OutOfMemoryError - si excede de mapa de bits recortado VM.
Mi método combina el suyo y la protección contra este excepcion: (l, t, r, b -% de imagen)
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;
}
y devuelve mapa de bits disponible max o nula
Gracias Steve, pero eso requeriría que cargue en el mapa de origen en primer lugar, y estoy tratando de evitar hacerlo, ya que es demasiado grande para caber en la memoria, y quiero trabajar con un mapa de bits que no haya sido muestreado . – Schermvlieger
Vaya, tiene razón, no leí esa parte de su pregunta correctamente. No estoy seguro de cómo hacer esto sin reducir la resolución. De hecho, ¡también hay un problema muy similar en mi propia lista de cómo hacer las cosas! –