直接上代码

//处理本地图片的方法

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;






}

更多相关文章

  1. 修改Android中Layout布局文件字体的大小
  2. android获取图片和视频的缩略图
  3. Android各代码层获取系统时间的方法
  4. Android:TextView与EditText中插入表情图片
  5. Dealing with Asset Compression in Android Apps(避免asset目录
  6. Android Studio 运行出现 Multiple dex files define Landroid/s
  7. android设置背景图片,去除背景图片
  8. Android 图片处理工具类封装

随机推荐

  1. Unity和Android通信系列文章1——总览
  2. Android百度地图(二):百度地图sdk显示位
  3. Android(安卓)系统菜单与自定义菜单
  4. 2021年Android高级面试题,终局之战
  5. Android:Kotlin详细入门学习指南-类和对象
  6. android 安装 出现Android(安卓)Native D
  7. 2021最新Android中高级开发必知必会面试
  8. WebView中的视频播放
  9. windows环境下生成 Android(安卓)可执行
  10. Android事件分发机制详解:史上最全面、最