BitmapFactory.decodeFile(imagePath) 为null

andy_swc 2015-07-14 05:13:20

private static final int VIDEO_WIDTH = 160;
private static final int VIDEO_HEIGHT = 200;
private Bitmap getImageThumbnail(String imagePath) {

System.out.println("getImageThumbnail - imagePath: " + imagePath);

Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //不申请内存 计算图片比例

// 获取这个图片的宽和高,注意此处的bitmap为null
bitmap = BitmapFactory.decodeFile(imagePath, options);

options.inJustDecodeBounds = false; //设为 false 申请内存
// 计算缩放比
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / VIDEO_WIDTH;
int beHeight = h / VIDEO_HEIGHT;
int be = 4;
if (beWidth < beHeight && beHeight >= 1) {
be = beHeight;
}
if (beHeight< beWidth && beWidth >= 1) {
be = beWidth;
}

if (be <= 0) {
be = 1;
} else if (be > 3) {
be = 3;
}

options.inSampleSize = be;
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
options.inPurgeable = true;
options.inInputShareable = true;
try {
// 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
bitmap = BitmapFactory.decodeFile(imagePath, options);

// 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
bitmap = ThumbnailUtils.extractThumbnail(bitmap, VIDEO_WIDTH, VIDEO_HEIGHT,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
} catch (OutOfMemoryError e) {
System.gc();
bitmap = null;
}

return bitmap;
}



07-14 16:58:37.924: I/System.out(22725): imCRadapter - position: 1
07-14 16:58:37.924: I/System.out(22725): imCRadapter - imagePath: /storage/emulated/0/chinahotline/myImage/IMG_20150707_1131557.png

07-14 16:58:37.924: I/System.out(22725): imCRadapter - setImageResource: 1
07-14 16:58:37.924: I/System.out(22725): getImageThumbnail - imagePath: /storage/emulated/0/chinahotline/myImage/IMG_20150707_1131557.png

07-14 16:58:37.954: I/System.out(22725): imCRadapter - position: 0
07-14 16:58:37.954: I/System.out(22725): imCRadapter - imagePath: /storage/emulated/0/chinahotline/myImage/IMG_20150706_1225S97.png





2张图片都是本地同一目录下的 而且在平板上指定文件夹中查看文件未损坏 但是加载缩略图时只有1张可以
具体
// 获取这个图片的宽和高,注意此处的bitmap为null
bitmap = BitmapFactory.decodeFile(imagePath, options);
这句代码返回为null 并没有内存溢出现象 网上找了半天没有找到有用的
...全文
978 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzc_a 2016-10-13
  • 打赏
  • 举报
回复
同样的问题,,,,,楼主解决了没有呀,,有三星手机上程序运行正常,但在华为上运行就不正常了
hxq4882 2015-07-14
  • 打赏
  • 举报
回复
当你,在选择图片时,由于内存太小到只 这个界面的activity 的 onsaveinstance 被调用 ,你因该在onsaveinstance里面讲图像path存储起来,当再次回到这个界面时候再区出来,然后再decodebitmap 就不会出现你的问题了
用于类似图库,缓存,所困、缩略图 package com.example.cache; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.lang.ref.SoftReference; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.util.DisplayMetrics; /** * 1.从内存中加载 * 2.本地缓存中加载 * 3.本地加载 * @author Administrator * */ public class LoadCacheImageTool { private Activity activity; private Map cacheMap; public LoadCacheImageTool(Activity activity){ this.activity = activity; this.cacheMap = new HashMap(); } public Bitmap loadCacheImage(String imagePath){ Bitmap bitmap = null; if (cacheMap.containsKey(imagePath)) { bitmap = cacheMap.get(imagePath).get(); if (bitmap!=null) { return bitmap; } } bitmap = loadLocalCacheImage(imagePath); cacheMap.put(imagePath, new SoftReference(bitmap)); return bitmap; } ///mnt/sdcard/bk.png ///mnt/sdcard/cache/bk.png.cache private Bitmap loadLocalCacheImage(String imagePath) { Bitmap bitmap = null; String cacheImagePath = getCacheImagePath(imagePath); File cacheFile = new File(cacheImagePath); if (!cacheFile.exists()) { bitmap = loadLocalBigImage(imagePath); saveToCacheDir(bitmap,cacheImagePath); }else{ try { bitmap = BitmapFactory.decodeStream(new FileInputStream(cacheFile)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return bitmap; } private String getCacheImagePath(String imagePath) { String cacheDir = new File(imagePath).getParent()+"/cache/"; if (!new File(cacheDir).exists()) { new File(cacheDir).mkdirs(); } String newImageName = new File(imagePath).getName()+".cache"; String newImagePath = cacheDir+newImageNam

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧