最有效的方式展现在Android上的局部图像 [英] The most efficient way to show the local image in android
问题描述
我目前喜欢的应用程序一家杂志社工作。由于存在放大(150%,200%,原始源的250%)的选项,我会preFER不按比例缩小的图像。但是,应用程序将强制重启,当我尝试去code图像,因为出来的内存。是否有任何建议,以解决这个问题?
图片是本地(SD卡),可检索的方式,但最终还是需要一个位图,因为我使用类似cavans.drawbitmap来显示它。我试过了,输入于流>字节,输入于流>位图等...但有什么最有效的方式,或者至少
我可以肯定的应用程序不强制重新启动/关闭?谢谢
{尝试
InputStream为=(InputStream的)新的URL(网址).getContent();
尝试{
返回BitmapFactory.de codeStream(是);
} {最后
is.close();
}
}赶上(例外五){
返回BitmapFactory.de codeResource(context.getResources(),defaultDrawable);
}
这是什么smartImageView(由循环J - 你可以找到他在 http://loopj.com/ )使用从驱动器/ SD检索文件。
私人位图getBitmapFromDisk(字符串imgID){
位图位图= NULL;
如果(diskCacheEnabled){
字符串文件路径= getFilePath(imgID);
档案文件=新的文件(文件路径);
如果(file.exists()){
位= BitmapFactory.de codeFILE(文件路径);
}
}
返回位图;
}
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);
}
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;
}
这篇关于最有效的方式展现在Android上的局部图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!