public class DriverHead { String path =Environment.getExternalStorageDirectory()+"/Picture/Myhead/"; public Bitmap getHard(String imageName) { String name=imageName+".jpg"; String url="http:/******************/"+imageName+".jpg";if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) { try { FileInputStream fis = new FileInputStream(path+name); return BitmapFactory.decodeStream(fis); } catch (FileNotFoundException e) { try { Bitmap bt=getHttpBitmap(url); saveFile(bt,name); return bt; }catch(Exception ev){ } } } else { Bitmap bt=getHttpBitmap(url); return bt; } return null; } public Bitmap getHttpBitmap(String url) { URL myFileUrl = null; Bitmap bitmap = null; try { myFileUrl = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setConnectTimeout(0); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } public void saveFile(Bitmap bm, String fileName) { try{ File dirFile = new File(path); if(!dirFile.exists()){ dirFile.mkdirs(); } File myCaptureFile = new File(path,fileName); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); bos.flush(); bos.close(); }catch(Exception e){ e.printStackTrace(); } } }
Android 保存图片到本地
最新推荐文章于 2024-09-24 18:16:47 发布