bitmap的用法(浅谈)
需求:从服务器下载一张图片,显示在ImageView控件上,并将该图片保存在移动设备的SD上。
步骤:
(一)获得输入流
//urlPath:服务器路径;
public InputStream getUrlInputStream(String urlPath) throws IOException{
URL url=new URL(urlPath);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
InputStream in=conn.getInputStream();
if(in!=null){
return in;
}else{
Log.i("test", "输入流对象为空");
return null;
}
}
(二)将输入流转化为Bitmap流
public Bitmap getMyBitmap(InputStream in){
Bitmap bitmap=null;
if(in!=null){
bitmap=BitmapFactory.decodeStream(in);
//BitmapFactory的作用:create Bitmap objects from various sources,including files,streams and byte-arrays;
return bitmap;
}else{
Log.i("test", "输入流对象in为空");
return null;
}
}
(三)给ImageView对象赋值
public void setWidgetImage(Bitmap bitmap){
ImageView img=new ImageView(this);
if(bitmap!=null){
img.setImageBitmap(bitmap);
}
}
(四)获取SD卡上的文件存储路径
public void createSDFile(){
File sdroot=Environment.getExternalStorageDirectory();
File file=new File(sdroot+"/Android/date/包名/文件名");
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
/**
* 相关操作
*/
}
}
第四步不是很很完美,File file=new File(sdroot+"/Android/date/包名/文件名");明显很麻烦,补一个方法如下:
public void getFileInfo(){
File skRoot = Environment.getExternalStorageDirectory();
File fileRoot = MainActivity.this.getFilesDir();
Log.i(tag + "判断SD卡是否插入", ""+Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED));
Log.i(tag + "获得sd卡根目录", ""+skRoot.getAbsolutePath());
Log.i(tag + "获得私有根目录", ""+fileRoot.getAbsolutePath());
Log.i(tag + "获得sd卡根目录文件或文件夹的名称", ""+skRoot.getName());
Log.i(tag + "获得私有根目录文件或文件夹的名称", ""+fileRoot.getName());
}
这样不管是得到程序的私有目录还是得到sd卡的目录就都有了,然后return就可以了
ps:if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))判断sd卡是否插上
注意:SD卡的权限
(五)将图片保存到SD卡上
public boolean readToSDCard(File file,Bitmap bitmap) throws FileNotFoundException{
FileOutputStream os=new FileOutputStream(file);
return bitmap.compress(Bitmap.CompressFormat.PNG, 90, os);
//bitmap.compress()的作用:write a compressed version of the bitmap to the specified outputstream;
//true:表示操作成功,false:表示操作失败
}