Sto tentando di salvare un layout in un'immagine nella scheda SD ma ottengo questo errore. Ho provato diversi codici che ho trovato in questo forum, ma tutti hanno la stessa chiamata di compressione che sta dando l'errore.Impossibile comprimere una bitmap riciclata
Questo è il codice che uso per salvare l'immagine:
private Bitmap TakeImage(View v) {
Bitmap screen = null;
try {
v.setDrawingCacheEnabled(true);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
screen = v.getDrawingCache();
v.setDrawingCacheEnabled(false); // clear drawing cache
} catch (Exception e) {
e.printStackTrace();
}
return screen;
}
E questo è il codice per salvarlo nella SDCard:
private void saveGraph(Bitmap graph, Context context) throws IOException {
OutputStream fOut = null;
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
fOut = new FileOutputStream(file);
graph.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), file.getName());
}
sto ottenendo l'errore:
Can't compress a recycled bitmap in the compress call!
Era quello! Ho tirato fuori quella linea e ha funzionato perfettamente! Grazie!!! – Lucia
Non si dovrebbe prendere la linea; la bitmap fornita dalla cache può essere riciclata in qualsiasi momento dalla vista che la possiede. Hai davvero bisogno di prendere la tua copia di Bitmap usando 'Bitmap.copy()'. –
Puoi spiegare come copiarlo? – Lucia