简体   繁体   English

应用效果后如何保存位图

[英]How to save a bitmap after applying effects

I have some buttons and one image view. 我有一些按钮和一个图像视图。 I load a photo from gallery,the photo is displayed on image view. 我从图库中加载了一张照片,该照片显示在图像视图中。 For every button,there is on click method to apply that applies one of the effects for the image. 对于每个按钮,都可以应用单击方法,该方法将为图像应用一种效果。 There is another button,that is for saving the photo on internal storage. 还有另一个按钮,用于将照片保存在内部存储器中。 I want to save the photo after one of the effects is applied. 应用其中一种效果后,我想保存照片。 I searched,but I just find how to save the photo without effect . 我进行了搜索,但是我只是找到了如何保存照片而没有任何效果。 Does somebody know how with the on click method to save the image AFTER the effect is applied . 有人知道应用效果后如何使用“单击”方法保存图像。

you can simply get the bitmap from ImageView by calling this function: 您只需调用以下函数即可从ImageView获取位图:

Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap()

and then save your bitmap where you want with in/out streams: 然后使用输入/输出流将位图保存在所需的位置:

private void saveBitmapFromImageView(File destFile, ImageView imageView){

    Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap()
    OutputStream os;
    try {
        os = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
    } catch(Exception e) {
        Log.e(TAG, "saveBitmapFromImageView", e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

Edit #1: 编辑#1:

IOUtils is Apache commons lib, add to your gradle: IOUtils是Apache commons lib,添加到您的gradle中:

compile 'org.apache.commons:commons-io:1.3.2'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM