Android保存Bitmap到本地

 private int i;

    public void saveMyBitmap(Bitmap mBitmap) {

        File dir = new File("/sdcard/Note/");
        if (!dir.exists())
            dir.mkdirs();
        File f = new File("/sdcard/Note/" + i++ + ".jpg");
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.e("FFF", e.getMessage());
        }
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("FFF", e.getMessage());
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("FFF", e.getMessage());
        }
    }

/**
     * @param bitmap  需要保存的图片对象
     * @param picPath 需要保存的本地路径,建议设置为公用常量
     */
    private void saveBitmap(Bitmap bitmap, String picPath) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(picPath));
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * @param imageView 需要设置图片的控件
     * @param picPath   需要读取图片的本地路径
     * @return
     */
    private void readBitmap(ImageView imageView, String picPath) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(picPath));
            Bitmap bitmap = BitmapFactory.decodeStream(fis);
            imageView.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值