如何在应用效果后保存位图

时间:2016-09-16 00:44:03

标签: java android android-bitmap android-internal-storage

我有一些按钮和一个图像视图。我从图库中加载了一张照片,照片显示在图像视图中。对于每个按钮,都有单击方法来应用,它会为图像应用其中一个效果。还有另一个按钮,用于将照片保存在内部存储器上。我想在应用其中一个效果后保存照片。我搜索过,但我发现如何保存照片没有效果。有人知道如何使用on click方法在应用效果后保存图像。

1 个答案:

答案 0 :(得分:0)

您只需调用此函数即可从ImageView获取位图:

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

然后使用输入/输出流将位图保存到所需的位置:

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

编辑#1:

IOUtils是Apache commons lib,添加到你的gradle:

compile 'org.apache.commons:commons-io:1.3.2'
相关问题