读取了一个图片,又新建了一个bitmap,然后使用bmp.setPixel(x, y, Color.argb(a, r, g, b))将新建的bitmap色彩填满,然后想要用新建图片保存覆盖原图片。
Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
使用如下方法保存,运行没问题,但是打开图片发现,新图片并没有覆盖原图片。读写权限没问题
FileOutputStream fos = new FileOutputStream(pic_url);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
又尝试使用如下字节流的方法保存,保存后直接图片无法读取了
byte pixels[] = new byte[bmp.getWidth() * bmp.getHeight() * 4];
bmp.copyPixelsToBuffer(ByteBuffer.wrap(pixels));
File bmpfile = new File(pic_url);
if (bmpfile.exists()) {
bmpfile.delete();
bmpfile.createNewFile();
}
FileOutputStream fos = new FileOutputStream(bmpfile);
fos.write(pixels);
fos.close();
重新读取图片时出现如下报错:
2023-03-02 23:17:27.181 12019-12019/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 12019
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
请问如何才能有效保存覆盖原图片?