Android---bitmap与file类型互换

来源:互联网 发布:网络拓扑图素材 编辑:程序博客网 时间:2024/06/04 17:42

首先是将bitmap保存为file类型

  1. 将bitmap和随便给图片起个名字传进去
public void saveFile(Bitmap bm, String fileName) {        try {            String path = getSDPath() + "/revoeye/";            File dirFile = new File(path);            if (!dirFile.exists()) {                dirFile.mkdir();            }            myCaptureFile = new File(path + fileName);            BufferedOutputStream bos;            bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));            bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);            bos.flush();            bos.close();        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }

获取 sd卡路径

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();    }

将file类型的图片转化为bitmap

String filePath="c:/01.jpg";//图片的路径可以用上边保存好的Bitmap bitmap=BitmapFactory.decodeFile(filePath,getBitmapOption(2)); //将图片的长和宽缩小味原来的1/2private Options getBitmapOption(int inSampleSize){        System.gc();        BitmapFactory.Options options = new BitmapFactory.Options();        options.inPurgeable = true;        options.inSampleSize = inSampleSize;        return options;}
0 0
原创粉丝点击