CreateBitmap() is null when full screen is enabled

All your problems with Audio, Video and Images.

CreateBitmap() is null when full screen is enabled

Postby srajpal » Tue Jan 13, 2009 8:35 pm

I am having an issue when trying to save a screen shot of a view to a file.

I am using the following code to save the bitmap

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. Bitmap screenshot;
  5.  
  6.  
  7.  
  8. mView.setDrawingCacheEnabled(true);
  9.  
  10. screenshot = Bitmap.createBitmap(mView.getDrawingCache());
  11.  
  12. mView.setDrawingCacheEnabled(false);
  13.  
  14.  
  15.  
  16. try {
  17.  
  18.        FileOutputStream fos = new FileOutputStream("/sdcard/test.png");
  19.  
  20.         screenshot.compress(Bitmap.CompressFormat.PNG, 100, fos);
  21.  
  22.         fos.close();           
  23.  
  24. } catch (FileNotFoundException e) {            
  25.  
  26.         Log.d("FileNotFoundException: " + e.getMessage());     
  27.  
  28. } catch (IOException e) {
  29.  
  30.         Log.d("IOException: " + e.getMessage());
  31.  
  32. }
  33.  
  34.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


it works fine unless i put the following in my onCreate() to make sure I am in full screen mode

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  3.  
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4



when that code is in there I get a NullPointerException at createBitmap()
does anybody know why this would be happening?
--
Sunny
srajpal
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jan 09, 2009 7:48 pm

Top

Postby beaster » Tue Jan 13, 2009 9:43 pm

using onMeasure().
beaster
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Oct 07, 2008 10:43 am

Postby srajpal » Tue Jan 13, 2009 10:15 pm

can you please elaborate
I do not see how onMeasure will do it
I tried to change the code to

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. screenshot = Bitmap.createBitmap(mView.getDrawingCache(),
  3.  
  4.                                 0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());
  5.  
  6.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


but I am still getting the same error.

I also tried to override onMeasure in the View but no go either

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  5.  
  6.                
  7.  
  8.                 Log.d("ScoreCardView","onMeasure("+widthMeasureSpec+","+heightMeasureSpec+")");
  9.  
  10.        
  11.  
  12.                 setMeasuredDimension(width,height);
  13.  
  14.                
  15.  
  16.                 Log.d("ScoreCardView","setMeasuredDimension("+width+","+height+")");
  17.  
  18.                
  19.  
  20.                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  21.  
  22.         }
  23.  
  24.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
--
Sunny
srajpal
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jan 09, 2009 7:48 pm

[SOLVED]

Postby srajpal » Wed Jan 14, 2009 10:20 pm

I was able to solve this, you have to create a Bitmap, give it to a Canvas and then draw your View into the Canvas. Then you can save the Bitmap. Now I can save the screen regardless if it is fullscreen or not.

here is the final code that saves the screenshot as a file:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //mView is my View
  2.  
  3.  
  4.  
  5. Bitmap screenBMP = Bitmap.createBitmap(mView.getMeasuredWidth(),
  6.  
  7.         mView.getMeasuredHeight(), Bitmap.Config.RGB_565);
  8.  
  9. Canvas screenShotCanvas = new Canvas(screenBMP);
  10.  
  11. mView.draw(screenShotCanvas);
  12.  
  13.  
  14.  
  15. try {
  16.  
  17.         FileOutputStream fos = new FileOutputStream("/sdcard/test.jpg");
  18.  
  19.         screenBMP.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  20.  
  21.         fos.close();
  22.  
  23.                
  24.  
  25. } catch (FileNotFoundException e) {
  26.  
  27.         Log.d("error",e.getMessage());
  28.  
  29. } catch (IOException e) {
  30.  
  31.         Lod.d("error",e.getMessage());
  32.  
  33. } catch (Exception e) {
  34.  
  35.         Log.d("error",e.getMessage()");
  36.  
  37. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
--
Sunny
srajpal
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jan 09, 2009 7:48 pm

Hi sunny,

Postby rajendrakumar » Mon Jan 26, 2009 6:53 am

I have seen your code.. can you please little bit expplain that what is my View.....
rajendrakumar
Developer
Developer
 
Posts: 40
Joined: Thu Jan 01, 2009 7:57 am

Postby srajpal » Mon Jan 26, 2009 4:09 pm

I meant mView is a View that I create and Draw in.
something like this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. //this is within the onCreate of the Activity
  3.  
  4. MyView mView = new MyView (this);
  5.  
  6. setContentView(mView);
  7.  
  8.  
  9.  
  10. //this is a View that I create and manipulate
  11.  
  12. public class MyView extends View {
  13.  
  14.  
  15.  
  16.      public MyView (Context context) {
  17.  
  18.         super(context);
  19.  
  20.     }
  21.  
  22.  
  23.  
  24.     @Override
  25.  
  26.     protected void onDraw(Canvas canvas) {
  27.  
  28.     }
  29.  
  30.  
  31.  
  32. }
  33.  
  34.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I hope this makes sense
--
Sunny
srajpal
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jan 09, 2009 7:48 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 15 guests