Copy bitmap issue
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 (Except开发者_开发知识库ion e) {
String strMsg = e.getMessage();
}
You can get a bitmap more easily with bitmapfactory.
How about this:
Bitmap bitmap = BitmapFactory.decodeFile("your file");
if (bitmap != null) {
Paint p = new Paint();
Canvas cv = new Canvas();
cv.drawBitmap(bitmap, 0, 0, p);
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strText = sDateFormat.format(new Date());
p.setColor(Color.RED);
cv.drawText(strText, 0, 0, p);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
}
精彩评论