public static File saveCanvasPictureToTempFile(Picture picture)
{
File tempFile = null;
// save to temporary file
File dir = getTempDir();
if(dir != null)
{
FileOutputStream fos = null;
try
{
File f = File.createTempFile("picture", ".stream", dir);
fos = new FileOutputStream(f);
picture.writeToStream(fos);
tempFile = f;
}
catch(IOException e)
{
Log.e(TAG, "failed to save picture", e);
}
finally
{
close(fos);
}
}
return tempFile;
}
이 코드는 임시 파일을 만들어 주 활동으로 반환 하겠지만 파일은 주 활동에서 널 포인터 예외를 제공합니다. 내가 뭘 잘못하고 있니? 내 주요 활동에 대한인쇄 이미지에 NUll 포인터 예외가 발생했습니다.
코드는
void printCanvasAsBitmapExample()
{
// create canvas to render on
Bitmap b = Bitmap.createBitmap(240, 240, Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);
// fill background with WHITE
c.drawRGB(0xFF, 0xFF, 0xFF);
// draw text
Paint p = new Paint();
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
p.setTextSize(18);
p.setTypeface(font);
p.setAntiAlias(true);
Rect textBounds = new Rect();
p.getTextBounds(HELLO_WORLD, 0, HELLO_WORLD.length(), textBounds);
int x = (c.getWidth() - (textBounds.right-textBounds.left))/2;
int y = (c.getHeight() - (textBounds.bottom-textBounds.top))/2;
c.drawText(HELLO_WORLD, x, y, p);
// draw icon
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
c.drawBitmap(icon, 0, 0, null);
// queue bitmap for printing
try
{
File f = PrintUtils.saveBitmapToTempFile(b, Bitmap.CompressFormat.PNG);
if(f != null)
{
PrintUtils.queueBitmapForPrinting(this, f, Bitmap.CompressFormat.PNG);
}
}
catch(Exception e)
{
Log.e(TAG, "failed to save/queue bitmap", e);
}
}
게시물에 logcat 오류를 넣을 수 있습니까? –
@Khush, 게시판 오류 또는 정확히 어디에서 오류가 발생하는지 언급하십시오 –