首页 > 解决方案 > 如何更改图像背景颜色并使用新背景颜色保存

问题描述

我有一个应用程序可以删除图像背景并设置新的蓝色背景颜色并保存它,但是当我打开图像时它显示黑色背景。

我怎样才能用蓝色背景保存这个?

   imageView.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));

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

    FileOutputStream outStream = null;
    File file = Environment.getExternalStorageDirectory();
    File dir = new File(file.getAbsolutePath() + "/Passport Photo123");
    dir.mkdirs();
    String fileName = String.format("%d.jpg", System.currentTimeMillis());
    File outFile = new File(dir, fileName);
    try {
        outStream = new FileOutputStream(outFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
    try {
        outStream.flush();
        outStream.close();
        Toast.makeText(this, "Photo Saved at: " + outFile, Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }

标签: android

解决方案


试试这行代码:-

imageView.setBackgroundColor(Color.rgb(100, 100, 50));

如果它不起作用,那么问题必须在其他地方。


推荐阅读