简体   繁体   中英

Loading lots of bitmaps in an social app without using java heap in android?

Basically I am making an image feed which is scrollable. And it works.But when memory profiling I don't think my users will be able to load lots of images without crashing.I was also doing memory profiling for Instagram's app and found out though they are loading lots of images but their heap size remains constant no matter how many images i have on my screen.There could be 3 possibilities

1)either they are using native memory for image rendering ,but I had tried implementing that but I am unable to render images without using the heap (if you can help me on this one it would be really cool )

2)they are using inSampleSize greater than 4 but that is really not possibe since the images are clear and medium quality

3)I smell native openGL interface in this , is it ?

Am I missing Out something !! please help !! I am using "ImageView" to load bitmaps


Even when memory profiling "Vine app" I found their java heap remains constant too whereas my java heap increase with the number of bitmaps loaded

I am using this library right now https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations

@Override
protected String doInBackground(String[] Params) {


    ParseFile fileObject = (ParseFile)photoObject.get("image");
    try {
        dataMain =  fileObject.getData() ;

        if(dataMain!= null) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inSampleSize = 1;
            options.inSampleSize = calculateInSampleSize(options, context.getResources().getDisplayMetrics().widthPixels,
                    context.getResources().getDisplayMetrics().widthPixels );

            options.inJustDecodeBounds = false;

            options.inPurgeable = true;

            bitmapHolder = new JniBitmapHolder(BitmapFactory.decodeByteArray(dataMain, 0, dataMain.length, options));

            bitmapHolder.scaleBitmap(widthPixels ,widthPixels* bitmapHolder.getBitmap().getHeight() / bitmapHolder.getBitmap().getWidth(), JniBitmapHolder.ScaleMethod.NearestNeighbour);

            //bmp = null ;

        }

    } catch (ParseException e) {
        e.printStackTrace();
    }


    }
        return "done";


}



    @Override
    protected void onPostExecute(String bitmap) {


            ImageView imageView = imageViewReference.get();

            if(imageView!= null && bitmapHolder.getBitmap() != null) {
                imageView.setImageBitmap(bitmapHolder.getBitmapAndFree());

                bitmapHolder = null;


            }

      }

Use Facebook Fresco . It uses ashmem to store images.

You may also try reusing bitmaps. Read official docs to get more about efficient bitmap loading.

Also consider using Glide or Picasso libraries instead of manual image loading.

我只是想得太深,发现回收者的观点就完成了工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM