获取图片的路径。。
private Bitmap getBitMapByPath(String path,int width,int height) throws FileNotFoundException, IOException
{ FileInputStream fis = new FileInputStream(path);
FileDescriptor fd = fis.getFD();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd, null, options);
options.inSampleSize = computeSampleSize(options,width,height); //这里调用压缩图片的函数
System.out.println("the inSampleSize is: " + options.inSampleSize);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
System.out.println("before decodefile");
Bitmap sourceBitmap = BitmapFactory.decodeFile(path, options); //这一行报内存溢出
System.out.println("after decodefile");
fis.close();
/* */ System.gc();
return sourceBitmap; }压缩函数
private int computeSampleSize(BitmapFactory.Options options, int width,int height) {
int w = options.outWidth;
int h = options.outHeight;
int candidatew = w / width;
int candidateh = h / height;
if(w / width != 0)
{
candidatew ++;
}
if(h / height != 0)
{
candidateh ++;
}
int candidate = Math.max(candidatew, candidateh);
if (candidate == 0)
return 1;
return candidate;
}
public void SetImage(String path)
{
try
{
FrontBitmap = getBitMapByPath(path,panelData.getWidth(),panelData.getHeight());//这里调用了上面第一个函数
if(FrontBitmap != null)
{
FrontScaleBitmap = Bitmap.createScaledBitmap(FrontBitmap, panelData.getWidth(), panelData.getHeight(), true);
/* */if(!FrontBitmap.isRecycled()){//先判断图片是否已释放了
FrontBitmap.recycle();
/* */System.gc();
System.out.println("运行了莫");
}
}
else
{
FrontScaleBitmap = null;
}
/* */if(!FrontBitmap.isRecycled()){//先判断图片是否已释放了
FrontBitmap.recycle();
System.out.println("要释放空间1111111111");
}
// /* */if(!FrontScaleBitmap.isRecycled()){//先判断图片是否已释放了
//
// FrontScaleBitmap.recycle();
// System.out.println("运行了莫1111111");
// }
if(imageView == null)
{
AbsoluteLayout.LayoutParams childLp = new AbsoluteLayout.LayoutParams(panelData.getWidth(), panelData.getHeight(), 0, 0);
imageView = new AnimaImageview(mOwnerLayout.getContext()); //new ImageView(mOwnerLayout.getContext());
mOwnerLayout.addView(imageView,childLp);
System.gc();
/* */if(!FrontBitmap.isRecycled()){//先判断图片是否已释放了
FrontBitmap.recycle();
System.out.println("要释放空间22222222222");
}
}
imageView.setImageBitmap(FrontScaleBitmap);
mOwnerLayout.bringChildToFront(imageView);
/* */System.gc();
if(oldFrontBitmap != null)
{
oldFrontBitmap.recycle();
oldFrontBitmap = null;
}
if(oldFrontScaleBitmap != null)
{
oldFrontScaleBitmap.recycle();
oldFrontScaleBitmap = null;
}
oldFrontBitmap = FrontBitmap;
oldFrontScaleBitmap = FrontScaleBitmap;
lastShowDate = Calendar.getInstance().getTime();
}
catch(Throwable te)
{
te.printStackTrace();
}
}
报错提示:
private Bitmap getBitMapByPath(String path,int width,int height) throws FileNotFoundException, IOException
{ FileInputStream fis = new FileInputStream(path);
FileDescriptor fd = fis.getFD();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd, null, options);
options.inSampleSize = computeSampleSize(options,width,height); //这里调用压缩图片的函数
System.out.println("the inSampleSize is: " + options.inSampleSize);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
System.out.println("before decodefile");
Bitmap sourceBitmap = BitmapFactory.decodeFile(path, options); //这一行报内存溢出
System.out.println("after decodefile");
fis.close();
/* */ System.gc();
return sourceBitmap; }压缩函数
private int computeSampleSize(BitmapFactory.Options options, int width,int height) {
int w = options.outWidth;
int h = options.outHeight;
int candidatew = w / width;
int candidateh = h / height;
if(w / width != 0)
{
candidatew ++;
}
if(h / height != 0)
{
candidateh ++;
}
int candidate = Math.max(candidatew, candidateh);
if (candidate == 0)
return 1;
return candidate;
}
public void SetImage(String path)
{
try
{
FrontBitmap = getBitMapByPath(path,panelData.getWidth(),panelData.getHeight());//这里调用了上面第一个函数
if(FrontBitmap != null)
{
FrontScaleBitmap = Bitmap.createScaledBitmap(FrontBitmap, panelData.getWidth(), panelData.getHeight(), true);
/* */if(!FrontBitmap.isRecycled()){//先判断图片是否已释放了
FrontBitmap.recycle();
/* */System.gc();
System.out.println("运行了莫");
}
}
else
{
FrontScaleBitmap = null;
}
/* */if(!FrontBitmap.isRecycled()){//先判断图片是否已释放了
FrontBitmap.recycle();
System.out.println("要释放空间1111111111");
}
// /* */if(!FrontScaleBitmap.isRecycled()){//先判断图片是否已释放了
//
// FrontScaleBitmap.recycle();
// System.out.println("运行了莫1111111");
// }
if(imageView == null)
{
AbsoluteLayout.LayoutParams childLp = new AbsoluteLayout.LayoutParams(panelData.getWidth(), panelData.getHeight(), 0, 0);
imageView = new AnimaImageview(mOwnerLayout.getContext()); //new ImageView(mOwnerLayout.getContext());
mOwnerLayout.addView(imageView,childLp);
System.gc();
/* */if(!FrontBitmap.isRecycled()){//先判断图片是否已释放了
FrontBitmap.recycle();
System.out.println("要释放空间22222222222");
}
}
imageView.setImageBitmap(FrontScaleBitmap);
mOwnerLayout.bringChildToFront(imageView);
/* */System.gc();
if(oldFrontBitmap != null)
{
oldFrontBitmap.recycle();
oldFrontBitmap = null;
}
if(oldFrontScaleBitmap != null)
{
oldFrontScaleBitmap.recycle();
oldFrontScaleBitmap = null;
}
oldFrontBitmap = FrontBitmap;
oldFrontScaleBitmap = FrontScaleBitmap;
lastShowDate = Calendar.getInstance().getTime();
}
catch(Throwable te)
{
te.printStackTrace();
}
}
报错提示:
int w = options.outWidth;
int h = options.outHeight;
int candidatew = w / width; = 0
int candidateh = h / height; = 0
if(w / width != 0)
{
candidatew ++;
}
if(h / height != 0)
{
candidateh ++;
}
int candidate = Math.max(candidatew, candidateh);
if (candidate == 0) //错误处:candidate 会始终为0 ,即你的图片没有压缩。
return 1;
return candidate;
} 你要使用压缩的话,该如下判断:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1; if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
具体代码可参考: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
里面有很多缓存图片,解析图片的例子。