@RKN
Il vostro metodo può anche lanciare OutOfMemoryError eccezione - se bitmap ritagliata supera VM.
Il mio metodo Distinti e la protezione contro questo combina exeption: (L, T, R, B -% dell'immagine)
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;
}
e restituisce max bitmap disponibile o nullo
Grazie Steve, ma per prima cosa dovrei caricarlo in sourceBitmap, e sto cercando di evitare di farlo, poiché è troppo grande per stare nella memoria, e voglio lavorare con una bitmap che non è stata sottoposta a downsampling . – Schermvlieger
Oops, hai ragione, non ho letto correttamente quella parte della tua domanda. Non sono sicuro di come farlo senza sottocampionare. In effetti, un problema molto simile si trova anche nella mia lista personale-come-fare-come-fare! –