标签:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public static String getSDPath() {File sdDir = null;boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);// 判断sd卡是否存在if (sdCardExist) {sdDir = Environment.getExternalStorageDirectory();// 获取跟目录}return sdDir.toString();}/*** 保存文件** @param bm* @param fileName* @throws IOException*/public void saveFile(Bitmap bm, String fileName) throws IOException {//获得路径String path = getSDPath() + "/revoeye/";File dirFile = new File(path);if (!dirFile.exists()) {dirFile.mkdirs();//保存路径}// 这里最好不要保存的名字都一样,容易出现imageLoader缓存的问题//path + fileName + ".jpg" 就是本地的URLFile myCaptureFile = new File(path + fileName + ".jpg");if(myCaptureFile.exists()){myCaptureFile.delete();}myCaptureFile.createNewFile();BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);bos.flush();bos.close();}
/**缩小图片到width和height的范围内** @param path 图片的位置* @param width 宽度* @param height 高度* @return*/public static Bitmap resizePhoto(String path, int width, int height) {BufferedInputStream in = null;try {in = new BufferedInputStream(new FileInputStream(new File(path)));BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeStream(in, null, options);int i = 0;Bitmap bitmap = null;while (true) {if ((options.outWidth >> i <= width) && (options.outHeight >> i <= height)) {in = new BufferedInputStream(new FileInputStream(new File(path)));options.inSampleSize = (int) Math.pow(2.0D, i);options.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeStream(in, null, options);break;}i += 1;}return bitmap;} catch (FileNotFoundException e) {e.printStackTrace();} finally {try {if (in != null) {in.close();}} catch (Exception e) {// ignore}}return null;}
Bitmap转为本地URL并存在Card && 本地URL转Bitmap
标签:
原文地址:http://www.cnblogs.com/fruitbolgs/p/4705567.html