/**     * Save Bitmap to a file.保存图片到SD卡。     *      * @param bitmap     * @param file     * @return error message if the saving is failed. null if the saving is     *         successful.     * @throws IOException     */    public static void saveBitmapToFile(Bitmap bitmap, String _file)            throws IOException {//_file = getSDPath()+"/xx自定义文件夹/hot.png"        BufferedOutputStream os = null;        try {            File file = new File(_file);            // String _filePath_file.replace(File.separatorChar +            // file.getName(), "");            int end = _file.lastIndexOf(File.separator);            String _filePath = _file.substring(0, end);            File filePath = new File(_filePath);            if (!filePath.exists()) {                filePath.mkdirs();            }            file.createNewFile();            os = new BufferedOutputStream(new FileOutputStream(file));            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);        } finally {            if (os != null) {                try {                    os.close();                } catch (IOException e) {                    Log.e(TAG_ERROR, e.getMessage(), e);                }            }        }    }
//读取public class MainAct extends Activity {        /** Called when the activity is first created. */        private ImageView img;        //SD图片路径        private String filepath = getSDPath()+"/xx自定义文件夹/hot.png"        @Override        public void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);                setContentView(R.layout.main);                img = (ImageView) findViewById(R.id.img);                File file = new File(filepath);                if (file.exists()) {                        Bitmap bm = BitmapFactory.decodeFile(filepath);                        //将图片显示到ImageView中                        img.setImageBitmap(bm);                }        }}
/** * 获取SDK路径 * @return */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();        }




更多相关文章

  1. Pycharm安装PyQt5的详细教程
  2. Android(安卓)matrix 控制图片的旋转、缩放、移动
  3. android拍照与读取相册
  4. Android(安卓)报错:Caused by: android.os.FileUriExposedExcepti
  5. Android--SoLoader,android动态加载so库
  6. Android(安卓)命令行编译、打包生成apk文件
  7. 创建android逐帧动画的两种方式
  8. android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
  9. [Android]在App中使用相机

随机推荐

  1. win10下mysql 8.0.12 安装及环境变量配置
  2. mysql 8.0.15 安装图文教程及数据库基础
  3. Win10下mysql 8.0.15 安装配置图文教程
  4. win10家庭版64位下mysql 8.0.15 安装配置
  5. MySQL在不知道列名情况下的注入详解
  6. MYSQL本地安装以及出现的问题解决
  7. mysql 8.0.15 安装配置图文教程
  8. MySQL5.7中的JSON基本操作指南
  9. win10下MySQL 8.0登录Access denied for
  10. mysql 8.0.15 安装配置方法图文教程