The most efficient way to show the local image in android

送分小仙女□ 提交于 2020-01-17 03:04:11

问题


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);


   }

回答1:


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




回答2:


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;
}


来源:https://stackoverflow.com/questions/20332476/the-most-efficient-way-to-show-the-local-image-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!