-4

I am just fetching an image from drawable and getting it into bitmap but while getting into bitmap its throws an error

java.lang.OutOfMemoryError: Failed to allocate a 1250874912 byte allocation with 16777216 free bytes and 338MB until OOM

Here is my code below

Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.drawable.vr_image);
    int size = icon.getByteCount();

Some solution found on stackoverflow but these also not working.

on manifest under application tag i have define following values

 android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:largeHeap="true"
    android:supportsRtl="true"

So any one can help me out from this problem not getting any solution for this.

Nikhil Sharma
  • 593
  • 7
  • 23
  • what are the dimension of your `vr_image` image? – WarrenFaith Jul 04 '17 at 09:41
  • 9999x3475 are dimension of image – Nikhil Sharma Jul 04 '17 at 09:42
  • and you consider `9999x3475` a normal size for an image on a mobile platform? – WarrenFaith Jul 04 '17 at 09:42
  • its an 360 image which i am using on my application so i have to consider it if i am picking from gallery so its working fine – Nikhil Sharma Jul 04 '17 at 09:43
  • @WarrenFaith actually i need to send this image to another phone using socket , so i have try to get uri of this image but its doen't work so i am fetching bitmap so that i can send this image – Nikhil Sharma Jul 04 '17 at 09:47
  • 2
    Then read the image like a normal file with a file stream and do not convert it to a bitmap first. That way you are loading the content of the bitmap into the memory which is roughly 1.2gb (based on the bytes in the error msg). Read it with a fix buffer size and send that buffer data via socket should be enough – WarrenFaith Jul 04 '17 at 09:59
  • Thanks for your answer i will just try it and thn update you with my output thanks! – Nikhil Sharma Jul 04 '17 at 10:30
  • Possible duplicate of [Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM](https://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio) – Abdul Waheed Jul 04 '17 at 10:40
  • @AbdulWaheed no it is not. The reason behind the question is (sadly only) in the comments and not in the question itself. It is not about loading an image to display it but rather just send it to another device. Therefore the error appeared only because the path the OP started to go was the wrong one. Within that context, the question is not a duplicate! – WarrenFaith Jul 04 '17 at 13:54
  • 1
    @WarrenFaith i have sloved the problem as you told me to thanks alot! – Nikhil Sharma Jul 04 '17 at 15:39
  • @Abdul thanks for your time but instead of pointing other mistake just try to slove it ok .. – Nikhil Sharma Jul 04 '17 at 15:39

2 Answers2

1

You can make use Picasso library. With the require pixel according to device you can resize and load into the imageview

 Picasso.with(context).load(incidentType.getIconServerPath()).error(R.drawable.no_image_icon).resize(mWidth, mWidth).onlyScaleDown().centerInside().into(holder.imageViewIcon,
                    new Callback() {
                        @Override
                        public void onSuccess() {
                            holder.relativeLayout.removeView(holder.progressBar);
                        }

                        @Override
                        public void onError() {
                            holder.relativeLayout.removeView(holder.progressBar);
                        }
                    });
        }
    });
samit
  • 168
  • 4
0

You are facing this problem because of the size of the image . It is too big either use library like ---http://square.github.io/picasso/ or decrease the size of image. better to use picasso .please dont increase size in the application it will effect the app performance.

Developer
  • 1
  • 6
  • but i dont need to put any view i just want an bitmap or uri coz i have ti send this image to another phone using socket – Nikhil Sharma Jul 04 '17 at 09:45