从网络获取图片保存到sdcard,以及加载为bitmap显示到ImageView

: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;
}

}



2Activity调用:

LoginInfodBiz biz=new LoginInfodBiz(context);
Bitmap bitLogo=biz.getPicFromSdcard(LoginInfodBiz.businesslogofile);
if(bitLogo==null){
return;
}else{
ivLogo.setImageBitmap(bitLogo);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,我们可以使用不同的方法来读取本地或网络图片并将其转换为Bitmap对象。 如果要读取本地图片,我们可以使用BitmapFactory类的decodeFile()方法。首先,我们需要获取图片的路径,并创建一个File对象,然后将其作为参数传递给decodeFile()方法来获取Bitmap对象。例如: ``` String imagePath = "/sdcard/image.jpg"; // 图片路径 File file = new File(imagePath); Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); ``` 如果要从网络中读取图片,我们可以使用网络请求库(如Volley或OkHttp)来发送网络请求并获取图片数据,然后使用BitmapFactory类的decodeByteArray()方法将其转换为Bitmap对象。首先,我们需要创建一个网络请求,并使用Response.Listener回调函数来处理请求成功的响应。在回调函数中,我们可以将响应的数据转换为Bitmap对象。例如: ``` String imageUrl = "http://example.com/image.jpg"; // 图片URL RequestQueue requestQueue = Volley.newRequestQueue(context); // 创建请求队列 ImageRequest imageRequest = new ImageRequest(imageUrl, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { // 处理响应成功的情况 // 在这里可以将response转换为Bitmap对象 Bitmap bitmap = response; // 使用Bitmap对象进行后续操作 } }, 0, 0, null, null); requestQueue.add(imageRequest); // 将请求添加到队列中 ``` 在将图片转换为Bitmap对象后,我们可以使用得到的Bitmap对象进行后续的操作,如显示ImageView上、保存到本地文件或进行图像处理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值