保存的位图是黑色的 [英] Saved Bitmap is black
本文介绍了保存的位图是黑色的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个带有文本的位图,可以在Imageview中查看它,但是当我保存位图时,我只会得到黑色图像.我花了三个小时来研究类似的问题,但没有一个对我有用.这是代码. 感谢您的帮助.
I created a bitmap with text and I can view it in an Imageview, but when I save the Bitmap I only get a black image. I have spend three hours looking at similar questions but none of the worked for me. Here is the code. Thanks for any help.
public void createBitmap(){
Bitmap LabelBitmap;
FileOutputStream fos = null;
//create Text Bitmap
LabelBitmap = textAsBitmap(this,"BRO D 0813","fonts/arialbd.ttf", 4, Color.BLACK);
//load bitmap in to Imageview
ImageView myImageView = (ImageView) findViewById(R.id.imageView);
myImageView.setImageBitmap(LabelBitmap);
// save bitmap
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/myfolder");
myDir.mkdirs();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
LabelBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
if (!myDir.exists()) {
myDir.mkdir();
}
File myDirFile = new File(root +"/myfolder/mybitmap.jpg");
try {
if(myDirFile.exists()){
myDirFile.delete();
}
myDirFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos = new FileOutputStream(myDirFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(bytes.toByteArray());
fos.flush();
fos.close();
Toast.makeText(this, "Image saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
推荐答案
JPEG
默认情况下,图像的背景为黑色,因此,如果您的文本颜色也为黑色,则会得到黑色图像.如果图像没有背景色,则必须将其另存为PNG
.进行以下更改并尝试:
JPEG
Image has a black background by default, so if your text color is also black you will get a black image. If your image has no background color, you must save it as PNG
. Change as following and have a try:
LabelBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
收件人:
LabelBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
这篇关于保存的位图是黑色的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文