user782104
user782104

Reputation: 13555

The most efficient way to show the local image in android

I am currently work on a magazine like apps. Since there is an option to zoom in(150%, 200%, 250% of original source) , I would prefer not to scale down the image. However , the app will force restart when I try to decode the image because of the out of memory. Are there any suggestion to fix that?

The image are local (SD card) , can be retrieve in any approach, but eventually need to be a bitmap as I use something like cavans.drawbitmap to display it. I tried, input stream-> bytes , input stream->bitmap etc... but are there any most efficient way or at least I can sure the app does not force restart / close? Thanks

 try {
            InputStream is = (InputStream) new URL(url).getContent();
            try {
                return BitmapFactory.decodeStream(is);
            } finally {
                is.close();                    
            }
        } catch (Exception e) {
            return BitmapFactory.decodeResource(context.getResources(), defaultDrawable);


   }

Upvotes: 0

Views: 391

Answers (2)

Aftab Safdar
Aftab Safdar

Reputation: 668

This is what smartImageView(by loopj - you can find him on http://loopj.com/) uses to retrieve files from the drive/sd.

private Bitmap getBitmapFromDisk(String imgID) {
    Bitmap bitmap = null;
    if(diskCacheEnabled){
        String filePath = getFilePath(imgID);
        File file = new File(filePath);
        if(file.exists()) {
            bitmap = BitmapFactory.decodeFile(filePath);
        }
    }
    return bitmap;
}

Upvotes: 1

Maciej Boguta
Maciej Boguta

Reputation: 1374

You should consider using nostra's image loader, it deals with memory quite efficiently, you can specify lots of config stuffs, im using it and its working pretty well for large images aswell

Upvotes: 1

Related Questions