android保存bitmap到sdcard

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //判断sdcard是否存在和是否具有读写权限
                    try{
                        String sdcard_path = Environment.getExternalStorageDirectory().getCanonicalPath();  //取得sdcard的路径
                        File dir=new File(sdcard_path+"/xinyuelantu");
                        if(!dir.exists()){  //创建目录
                            dir.mkdir();
                        }
                        dir=new File(sdcard_path+"/xinyuelantu/browser");
                        if(!dir.exists()){
                            dir.mkdir();
                        }
                        dir=new File(sdcard_path+"/xinyuelantu/browser/favicon");
                        if(!dir.exists()){
                            dir.mkdir();
                        }

                        Calendar calendar= Calendar.getInstance(); //获取当前时间作为图标名字
                        String year=calendar.get(Calendar.YEAR)+"";
                        String month=calendar.get(Calendar.MONTH)+1+"";
                        String day=calendar.get(Calendar.DAY_OF_MONTH)+"";
                        String hour=calendar.get(Calendar.HOUR)+"";
                        String minute=calendar.get(Calendar.MINUTE)+"";
                        String second=calendar.get(Calendar.SECOND)+"";
                        String time=year+month+day+hour+minute+second;

                        File file=new File(sdcard_path+"/xinyuelantu/browser/favicon/"+time+".jpg");
                        file.createNewFile();  //创建文件
                        FileOutputStream fileOutputStream=new FileOutputStream(file);  //获得文件句柄
                        Bitmap favicon=webview.getFavicon();   //得到当前网站的图标
                        favicon.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);  //保存网站图标,第二个参数取值为0-100,100表示最高质量
                        fileOutputStream.flush();
                        fileOutputStream.close();
                    }
                    catch(IOException e){}
                }

 

posted @ 2016-05-05 15:00  zqxLonely  阅读(202)  评论(0编辑  收藏  举报