复制位图问题
我有一个问题要问。我只想在以下代码中将文本添加到位图中。当我调试到 Bitmap bmp = bmp1.copy(bmp1.getConfig(), true); 监视窗口中的一切正常时,我可以看到宽度和高度为 640 和 480。
但是在执行复制方法之后它返回位图为bmp,但bmp的宽度和高度为-1和-1,并且无法添加文本。有人可以告诉我为什么以及如何解决这个问题吗?多谢
String strPath=Environment.getExternalStorageDirectory().toString();
String strFileNameIn="aaa.jpg";
File inFile=new File(strPath+File.separator+strFileNameIn);
try {
if (inFile.exists()) {
FileInputStream inStream = new FileInputStream(inFile);
BitmapDrawable bmpDraw = new BitmapDrawable(inStream);
Bitmap bmp1 = bmpDraw.getBitmap();
Bitmap bmp = bmp1.copy(bmp1.getConfig(), true);
Canvas cv = new Canvas(bmp);
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strText = sDateFormat.format(new Date());
Paint p = new Paint();
p.setColor(Color.RED);
cv.drawText(strText, 0, 0, p);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
}
} catch (Exception e) {
String strMsg = e.getMessage();
}
I've got a question to ask. I just want to add a text to a bitmap in following code. when I debug to Bitmap bmp = bmp1.copy(bmp1.getConfig(), true);
everything worked fine in watch window I can see width and height with 640 and 480.
but after excute the copy method it returns the Bitmap to bmp, but the width and height of bmp is -1 and -1, and it couldn't add the text. could some guys told me why and how to fix the problem. thanks a lot
String strPath=Environment.getExternalStorageDirectory().toString();
String strFileNameIn="aaa.jpg";
File inFile=new File(strPath+File.separator+strFileNameIn);
try {
if (inFile.exists()) {
FileInputStream inStream = new FileInputStream(inFile);
BitmapDrawable bmpDraw = new BitmapDrawable(inStream);
Bitmap bmp1 = bmpDraw.getBitmap();
Bitmap bmp = bmp1.copy(bmp1.getConfig(), true);
Canvas cv = new Canvas(bmp);
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strText = sDateFormat.format(new Date());
Paint p = new Paint();
p.setColor(Color.RED);
cv.drawText(strText, 0, 0, p);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
}
} catch (Exception e) {
String strMsg = e.getMessage();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 bitmapfactory 更轻松地获取位图。
这个怎么样:
You can get a bitmap more easily with bitmapfactory.
How about this: