关于如何实现下载图片并保存到手机上,这个链接上写的很清楚点击打开链接,需要说明的是很多操作需要加异常处理,比如读入网上图片,保存图片都要加try/catch,另外像访问图片等需要联网或者耗时的操作都不能在主线程运行,需要要新开子线程。保存图片(点击打开链接):
public void saveFile(Bitmap bm)throws IOException{ //Environment.getExternalStorageDirectory() ; 获得SDCard目录 File dirFile = new File(Environment.getExternalStorageDirectory() + "文件夹"); if(!dirFile.exists()){ dirFile.mkdir(); } File myCaptureFile = new File(Environment.getExternalStorageDirectory() + "文件夹" + "文件名(可以加格式)"); //new FileOutputStream(myCaptureFile) 获取要保存到的文件的文件流 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); //把指定的bitmp压缩到文件中 就是保存在指定文件中 format是文件格式(Bitmap.CompressFormat.JPEG jpeg) // quality 是品质(100 就是原质量) bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); bos.flush(); bos.close(); }
关于读取图片并显示的代码如下(点击打开链接):
private Bitmap read() throws FileNotFoundException {
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/test3/" + "pic1",null);
return bitmap;
}