如何更改图像背景色并将其保存为新的背景色

时间:2018-10-31 06:32:54

标签: android

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

我如何用蓝色背景保存它?。

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

1 个答案:

答案 0 :(得分:0)

尝试以下代码:-

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

如果这行不通,那么问题一定出在其他地方。