4条回答 默认 最新
- JPF1024 2015-12-17 01:24关注
参考:
/** * 保存图片到本地 * <p/> * <p>需要权限:</p>{@code * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> * <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>} * * @param bitmap 要保存的图片. * @throws IOException 写文件时导致的异常. * @throws FileNotFoundException 文件未找到时,或路径不存在时抛出此异常. * @see {@link com.w1520.liangye.utils.NetworkUtils#saveImages(ImageView imgView)} */ public void saveImages(Bitmap bitmap) { String strPath = getSDPath(); DateUtils dateutils = DateUtils.getInstance(); String strFileName = dateutils.getCurrentTimeById() + ".jpg"; FileOutputStream fos = null; try { File destDir = new File(strPath); if (!destDir.exists()) { destDir.mkdirs(); } final String filePaths = strPath + "/" + strFileName; File imageFile = new File(filePaths); imageFile.createNewFile(); fos = new FileOutputStream(imageFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos); fos.flush(); showToast("已成功保存到相册", Toast.LENGTH_SHORT); refreshPicture(filePaths); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 获取SD卡或者内置存储空间可以保存资源的路径. * <em>此处未实现对存储空间是否充足进行判断</em>. * * @return 返回保存数据的路径, 有SD卡则是SD上的路径, 反之内置存储空间上的路径. */ private String getSDPath() { boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); if (hasSDCard) { return Environment.getExternalStorageDirectory().toString() + "/Pictures/liangye"; } else return "/data/data/package"; }
更多:
http://www.07q.net/read/2381.html
下面的网络工具类。本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报