从网络获取图片保存到sdcard,以及加载为bitmap显示到ImageView
来源:互联网 发布:苏州嘉图软件 编辑:程序博客网 时间:2024/05/22 12:01
:1:业务类
public class LoginInfodBiz {
private Context context;
public static final String businesslogofile=Environment.getExternalStorageDirectory().toString()+"/business/image/logo.png";
public LoginInfodBiz(Context context) {
this.context=context;
}
/**
* 从网络获取图片并保存到sdcard
* @param url
*/
public void savaBusinessLogo(final String url){
new Thread(new Runnable() {
@Override
public void run() {
try {
Bitmap bitmap = null;
URL picUrl=new URL(url);
InputStream in=picUrl.openStream();
bitmap=BitmapFactory.decodeStream(in);
in.close();
savePictrue(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private void savePictrue(Bitmap bitmap) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
FileOutputStream out = null;
try {
File file=new File(businesslogofile);
// 如果父目录不存在 则创建
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 如果文件不存在 则创建文件
if (!file.exists()) {
file.createNewFile();
}
// 保存图片到文件
bitmap.compress(CompressFormat.PNG, 100, new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (out !=null) out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 从sdccard获取图片
* @param context
* @param path
* @return
*/
public Bitmap getPicFromSdcard(String pathFile){
Bitmap bitmap=null;
try {
File file=new File(pathFile);
FileInputStream fis = new FileInputStream(file);
bitmap=BitmapFactory.decodeStream(fis);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
public class LoginInfodBiz {
private Context context;
public static final String businesslogofile=Environment.getExternalStorageDirectory().toString()+"/business/image/logo.png";
public LoginInfodBiz(Context context) {
this.context=context;
}
/**
* 从网络获取图片并保存到sdcard
* @param url
*/
public void savaBusinessLogo(final String url){
new Thread(new Runnable() {
@Override
public void run() {
try {
Bitmap bitmap = null;
URL picUrl=new URL(url);
InputStream in=picUrl.openStream();
bitmap=BitmapFactory.decodeStream(in);
in.close();
savePictrue(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private void savePictrue(Bitmap bitmap) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
FileOutputStream out = null;
try {
File file=new File(businesslogofile);
// 如果父目录不存在 则创建
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 如果文件不存在 则创建文件
if (!file.exists()) {
file.createNewFile();
}
// 保存图片到文件
bitmap.compress(CompressFormat.PNG, 100, new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (out !=null) out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 从sdccard获取图片
* @param context
* @param path
* @return
*/
public Bitmap getPicFromSdcard(String pathFile){
Bitmap bitmap=null;
try {
File file=new File(pathFile);
FileInputStream fis = new FileInputStream(file);
bitmap=BitmapFactory.decodeStream(fis);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
}
2Activity调用:
LoginInfodBiz biz=new LoginInfodBiz(context);
Bitmap bitLogo=biz.getPicFromSdcard(LoginInfodBiz.businesslogofile);
if(bitLogo==null){
return;
}else{
ivLogo.setImageBitmap(bitLogo);
}
0 0
- 从网络获取图片保存到sdcard,以及加载为bitmap显示到ImageView
- 将bitmap保存到sdcard
- android保存bitmap到sdcard
- 从网络url读取图片加载到imageview
- 如何从网络获取图片显示并保存到SD卡里
- 如何从网络获取图片显示并保存到SD卡里
- Android实现从网络获取图片显示并保存到SD卡的方法
- iOS 从相册加载图片到imageview
- 从网络获取图片并保存到手机相册,保存成功但是相册不显示图片问题
- Android 从网络获取图片保存到SD卡中
- Qt 获取网络gif图片并保存到本地显示
- ImageView从SdCard中获取图片
- Android数据存储:获取网络图片把图片保存到SD卡中并从SDk卡中读取(通过Bitmap方法)
- Glide加载图片并保存到本地返回file,bitmap
- Glide加载图片并保存到本地返回file,bitmap
- Android4.4以后从相册中获取图片显示到Imageview上实现
- 获取网络图片,并保存到本地
- 【Android开发】范例1-开启新线程获取网络图片并显示到ImageView中
- bmp文件格式详细解析
- 利用rsync同步多台机器文件到同一台
- 深入JVM之类加载、类加载器、双亲委派机制与常见问题
- STM32输入输出模式理解
- Python借助Openpyxl读写excel2007 +
- 从网络获取图片保存到sdcard,以及加载为bitmap显示到ImageView
- android app启动另外一个app
- quick-3.5 lua调用c++
- <转载> 关于python的装饰器
- 2012年5月SAT香港真题解析
- C++学习笔记 堆与栈的区别
- 首选项的四种模式
- PHP1-1 php编码规范及变量符号介绍
- 【Java】编写函数,检查链表是否为回文