Bitmap对象转文件

图片路径为根目录/a1/时间戳.jpg

 public File saveBitmap(Bitmap bitmapUse) {
        Log.e("wy", "开始保存");
        //生成一个文件,存储我们将来拍照的照片
        String sdPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/a1";
        File file = new File(sdPath);

        Log.e("wy","绝对文件路径: "+ file.getAbsoluteFile());
        Log.e("wy","文件名: "+ file.getName());
        if (!file.exists()) {
            file.mkdirs();
            Log.e("wy", "创建文件夹,路径:"+file.getPath());
        }
        String mPath = System.currentTimeMillis() + "";
        Log.e("wy", "保存路径: " + sdPath);
        File f = new File(sdPath, mPath + ".jpg");
//        File f = new File("/sdcard/namecard/", picName);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
//            10M
//            bitmapUse.compress(Bitmap.CompressFormat.PNG, 90, out);
//            0.5M
            bitmapUse.compress(Bitmap.CompressFormat.JPEG, 45, out);
            out.flush();
            out.close();
            Log.e("wy", "已经保存");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return f;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章