Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:59:12+00:00 2026-05-27T23:59:12+00:00

public static File saveCanvasPictureToTempFile( Picture picture ) { File tempFile = null; // save

  • 0
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;
}

This code is supposed to create a temp file and return it to the Main activity, but the file is giving me a null pointer exception in the main activity. What possibly am i doing wrong?

The code for my main activity is

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 );
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:59:13+00:00Added an answer on May 27, 2026 at 11:59 pm

    Your error is possibly due to the usage of fos = new FileOutputStream(f) instead of the Android specific way of using fos = [context instance].openFileOutput(filename, mode).

    It might be that you do not have permissions to write to the tempDir, and so you get a NullPointerException.

    Refer to the documentation. It lays it out quite clearly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the way I read file: public static string readFile(string path) { StringBuilder
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
I have the following method in an App_Code/Globals.cs file: public static XmlDataSource getXmlSourceFromOrgid(int orgid)
Following is my method to append one file onto another file. public static void
When I start my application I create a temp folder: public static File createTempDir(String
I found some code on here for creating temporary directories in Java. public static
I am using this method for encrypting a video file: public static void encryptToBinaryFile(String
I am using following code to get List from xml file - public static
I have this code for encrypting video files. public static void encryptVideos(File fil,File outfile)
I have the following method of copying files: public static void nioCopy(File source, File

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.