【发布时间】:2022-01-31 10:56:10
【问题描述】:
我正在尝试生成一个带有签名的 png 文件。所以我希望它是透明背景,但我所能做的就是生成一个白色背景的签名,这不是我想要的输出。
这是我的代码。以防我遗漏了什么。
File file = new File(getExternalFilesDir(null), IndividualFragment.lastname.getText().toString().concat("_").concat(IndividualFragment.firstname.getText().toString()).concat("_sig") + ".png");
FileOutputStream out = null;
Bitmap bitmap = signatureView.getSignatureBitmap();
try {
out = new FileOutputStream(file);
if (bitmap != null) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} else {
throw new FileNotFoundException();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
if (bitmap != null) {
Toast.makeText(getApplicationContext(),
"Image saved successfully at " + file.getPath(),
Toast.LENGTH_LONG).show();
AttachmentsFragment.appPath.setText(file.getPath());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
new MyMediaScanner(this, file);
} else {
ArrayList<String> toBeScanned = new ArrayList<String>();
toBeScanned.add(file.getAbsolutePath());
String[] toBeScannedStr = new String[toBeScanned.size()];
toBeScannedStr = toBeScanned.toArray(toBeScannedStr);
MediaScannerConnection.scanFile(this, toBeScannedStr, null,
null);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
在此先感谢各位,非常感谢您的帮助。
【问题讨论】:
-
用透明替换背景颜色。换色方法见this post
-
没有生效
-
这能回答你的问题吗? Bitmap loses transparency when it's saved
-
很遗憾没有
-
使用这个github.com/gcacace/android-signaturepad得到这个工作