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

  • Home
  • SEARCH
  • 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 7792007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:03:58+00:00 2026-06-01T22:03:58+00:00

Hi StackOverflow community, in my application I’d like to have a button which allows

  • 0

Hi StackOverflow community,

in my application I’d like to have a button which allows the user to capture the current view.

For that reason, I’ve written the following method (oriented towards: Android save view to jpg or png ):

private LinearLayout container; 

public void createImageOfCurrentView(View v) {
    if (isExternalStoragePresent()) {
        container = (LinearLayout) findViewById(R.id.container);
        container.setDrawingCacheEnabled(true);
        Bitmap b = container.getDrawingCache();

        File dir = new File(Environment.getExternalStorageDirectory().getPath()
          + "/" + getPackageName() + "/");
        dir.mkdirs();

        File file = new File(dir, "image.jpg");
        FileOutputStream fos;

        try {
            fos = new FileOutputStream(file);
            b.compress(CompressFormat.JPEG, 95, fos); // NullPointerException!
            Toast.makeText(this, "The current view has been succesfully saved "
              + "as image.", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            Toast.makeText(this, "Unfortunatly an error occured and this "
              + "action failed.", Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(this, "Bacause of lacking access to the storage "
          + "of this device, the current view couldn't be saved as an image.",
          Toast.LENGTH_LONG).show();
    }
}

The problem is according to LogCat that there occured a NullPointerException when trying to create the jpeg. So probably the method container.getDrawingCache() is not working. On the phone the file is generated. However with the size of 0 bytes and no visible image. I would appreciate any suggestions what I have to do in order to make it work as I want.

  • 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-06-01T22:03:59+00:00Added an answer on June 1, 2026 at 10:03 pm

    Try it –

    public static Bitmap convertViewToBitmap(View view) {
       Bitmap result = null;
    
       try {
          result = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
          view.draw(new Canvas(result));
       }catch(Exception e) {}    
       return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello StackOverflow community, I have run into a problem that quite frankly is baffling
Dear stackoverflow community, I have a simple menu that have to be dynamics. With
Dear members of the Stackoverflow community, We are developing a web application using the
I have an nascent iPad application, which stores documents internally on the device in
Thought I would ask the knowledgeable StackOverflow community a question that was on my
I would like to get the stackoverflow community's opinion on the following three design
Hey StackOverflow community, My question is as follows: I have a table, say USER_ADDR
I have a decision to make and I'm kicking it to the stackoverflow community.
Hello, stackoverflow community! I am working on a rather large database-driven web application. The
I have a time critical application that needs to send a UDP datagram on

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.