Android – Come convertire foto da webview.capturePicture () a byte e tornare a bitmap
Sto cercando di catturare l'image che sto ottenendo da webview.capturePicture () per salvarlo in una sqliteDatabase, per fare che ho bisogno di convertire l'image in un byte [] per poterlo salvarlo come BLOB nella mia tabella , e quindi in grado di recuperare quel byte [] e convertirlo in bitmap.
Ecco cosa sto facendo:
Picture p = webView.capturePicture(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); p.writeToStream(bos); byte[] ba = bos.toByteArray());
Riprova l'image da:
byte[] image = cursor.getBlob(imageColumnIndex); Bitmap bm = BitmapFactory.decodeByteArray(image, 0, image.length);
Sono in grado di recuperare il byte [] solo bene, ma ho un bitmap null tutto il tempo da bitmapfactory.
Ho anche notato che se io log.d (TAG, "+ bos") ottengo una lunga sequenza di byte come previsto, ma se faccio lo stesso a ba subito dopo che faccio bos.toByteArray (), ho solo una breve matrix, qualche cosa come questa: [B @ 2b0a7c60
Sto indovinando di aver difficoltà a convertire da OutputStream in byteArray. Potrebbe questo per perché il metodo capturePiture () restituisce un OutputStream invece di un ByteArrayOutputStream?
Qualsiasi aiuto sarebbe apprezzato.
One Solution collect form web for “Android – Come convertire foto da webview.capturePicture () a byte e tornare a bitmap”
Utilizza il seguente converter function ::::
public String convertBitmapToString(Bitmap src) { String str =null; if(src!= null){ ByteArrayOutputStream os=new ByteArrayOutputStream(); src.compress(android.graphics.Bitmap.CompressFormat.PNG, 100,(OutputStream) os); byte[] byteArray = os.toByteArray(); str = Base64.encodeToString(byteArray,Base64.DEFAULT); } return str; } public static Bitmap getBitMapFromString(String src) { Bitmap bitmap = null; if(src!= null){ byte[] decodedString = Base64.decode(src.getBytes(), Base64.DEFAULT); bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length); } return bitmap; }
aggiornato ::
//Convert Picture to Bitmap private static Bitmap pictureDrawable2Bitmap(Picture picture){ PictureDrawable pictureDrawable = new PictureDrawable(picture); Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawPicture(pictureDrawable.getPicture()); return bitmap; }