1、从本地加载图片
/**
* 加载本地图片
* @param imagePath图片路径
* @return
*/
public Bitmap loadImageFromLocal(String imagePath){
Bitmap img = null;
File file = new File(imagePath);
if(file.exists()){
BitmapDrawable bitmap = new BitmapDrawable(imagePath);
img = bitmap.getBitmap();
}
return img;
}
2、保存图片到本地
/**
* 保存图片
* @param bm
* @param filePath
* @throws IOException
*/
public void saveBitmap(Bitmap bm, String filePath) throws IOException {
File myCaptureFile = new File(filePath);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();
}
3、加载网络图片
/**
* 下载网络图片
* @param url
* @param width
* @param height
* @return
*/
private Bitmap downloadBitmap(String url, int width, int height) {
try {
Bitmap bitmap = null;
url.replaceAll(” “, “%20″);
bitmap = BitmapFactory.decodeStream((InputStream) new URL(
url).getContent());
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
原创内容转载请保留出处GEEK笔记(https://www.geekapp.cn/)。