android处理大图片内存溢出的简单办法

直接上代码

//处理本地图片的方法

public static Bitmap getBtimap(String path){

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds=true;
BitmapFactory.decodeFile(path,opts);
int imageH=opts.outHeight;
int imageW=opts.outWidth;
int scale=1;
int scaleX=imageW/Resolution.getInstance().getScreenWidth(),scaleY=imageH/Resolution.getInstance().getScreenHeight();
if (scaleX>=scaleY & scaleY>=1) {
scale=scaleX;
}
if (scaleY>=scaleX & scaleX>=1) {
scale=scaleY;
}
opts.inJustDecodeBounds=false;
opts.inSampleSize = scale;
opts.inTempStorage = new byte[16 * 1024];
opts.inPurgeable = true;
 opts.inInputShareable = true;
 opts.inDither = false;
 opts.inPurgeable = true;


FileInputStream is = null;
try {
is = new FileInputStream(path);
Bitmap bmp;
bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);

return bmp;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.gc();
}
}





// Bitmap bitmap = BitmapFactory.decodeFile(f.getPath(),opts);
// opts.inBitmap=bitmap;

return null;



}

//下面方法是处理网络图片的
public static Bitmap getBtimap(URL url){
//校验文件完整性
InputStream is = null;
try {
is = url.openStream();
byte[] bytes = getBytes(is); 
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds=true;
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
int imageH=opts.outHeight;
int imageW=opts.outWidth;
int scale=0;
int scaleX=imageW/Resolution.getInstance().getScreenWidth(),scaleY=imageH/Resolution.getInstance().getScreenHeight();
if (scaleX>=scaleY & scaleY>=1) {
scale=scaleX;
}
if (scaleY>=scaleX & scaleX>=1) {
scale=scaleY;
}
opts.inJustDecodeBounds=false;
opts.inSampleSize = scale;
   System.out.println("urlpicture"+scale);
  Bitmap decodeBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);


return decodeBitmap;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
return null;
} finally{
if (is!=null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;





}
private static byte[] getBytes(InputStream is) { 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
byte[] b = new byte[2048]; 
int len = 0; 
try { 
while ((len = is.read(b, 0, 2048)) != -1) { 
baos.write(b, 0, len); 
baos.flush(); 

} catch (IOException e) { 
e.printStackTrace(); 

byte[] bytes = baos.toByteArray(); 
return bytes; 

// 处理Assert 文件目录下图片

  public static Bitmap getAssertImage(Context context,String name) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds=true;
//InputStream fIn = new FileInputStream(f.getPath());


        try {
         BitmapFactory.decodeStream(context.getAssets().open(name), null, opts);


        } catch (IOException e) {
            e.printStackTrace();
        }
        int imageH=opts.outHeight;
int imageW=opts.outWidth;
int scale=1;
int scaleX=imageW/Resolution.getInstance().getScreenWidth(),scaleY=imageH/Resolution.getInstance().getScreenHeight();
if (scaleX>=scaleY & scaleY>=1) {
scale=scaleX;
}
if (scaleY>=scaleX & scaleX>=1) {
scale=scaleY;
}
opts.inJustDecodeBounds=false;
opts.inSampleSize = scale;
opts.inTempStorage = new byte[16 * 1024];
opts.inPurgeable = true;
 opts.inInputShareable = true;
 opts.inDither = false;
 opts.inPurgeable = true;


InputStream is = null;
try {
is = context.getAssets().open(name);
Bitmap bmp;
bmp = BitmapFactory.decodeStream(is, null, opts);


return bmp;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.gc();
}
}






// Bitmap bitmap = BitmapFactory.decodeFile(f.getPath(),opts);
// opts.inBitmap=bitmap;


return null;






}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值