Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent out of memory exception while trying to convert input stream object into bitmap in android?
    primarykey
    data
    text
    <p>I had used following code to convert inputstream object into bitmap. But it returns "out of memory error", and BitmapFactory Options always returns Zero.</p> <pre><code>S3ObjectInputStream inputStreamReceiptObject = objectReceiptFromAmazonS3 .getObjectContent(); Bitmap bitmapImageFromAmazon = null; try { if (inputStreamReceiptObject != null){ BitmapFactory.Options o = new BitmapFactory.Options(); o.inSampleSize = 8; o.inJustDecodeBounds = true; bitmapImageFromAmazon = BitmapFactory.decodeStream(inputStreamReceiptObject,null,o); // o is always null if(bitmapImageFromAmazon == null){ System.out.println("Bitmap null"); } } </code></pre> <p>Advance Thanks for any help !</p> <p>SOLUTION : ( Lot of thanks to Honourable Don and Honourable Akshat )</p> <pre><code> ByteArrayOutputStream baos = null ; InputStream is1 = null,is2 = null; try { baos = new ByteArrayOutputStream(); // Fake code simulating the copy // You can generally do better with nio if you need... // And please, unlike me, do something about the Exceptions :D byte[] buffer = new byte[1024]; int len; while ((len = inputStreamReceiptObject.read(buffer)) &gt; -1 ) { baos.write(buffer, 0, len); } baos.flush(); // Open new InputStreams using the recorded bytes // Can be repeated as many times as you wish is1 = new ByteArrayInputStream(baos.toByteArray()); is2 = new ByteArrayInputStream(baos.toByteArray()); bitmapImageFromAmazon = getBitmapFromInputStream(is1,is2); if(bitmapImageFromAmazon == null) System.out.println("IMAGE NULL"); else System.out.println("IMAGE NOT NULL"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { baos.close(); is1.close(); is2.close(); } public Bitmap getBitmapFromInputStream(InputStream is1,InputStream is2) throws IOException { Bitmap bitmap = null; try { //Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(is1,null,o); //Find the correct scale value. It should be the power of 2. int scale=1; //Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize=scale; bitmap = BitmapFactory.decodeStream(is2, null, o2); } catch (Exception e) { e.printStackTrace(); } return bitmap; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload