0

I'm trying to apply this code to get a screenshot of a view without an actionbar:

 View main = findViewById(R.id.view);
Bitmap screenshot;
main.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(main.getDrawingCache());
main.setDrawingCacheEnabled(false);

My question is how i can get the view if we don't have a layout,the view is set like:

setContentView(new MyView(this));

please take a look at this project: https://github.com/valerio-bozzolan/AcrylicPaint/blob/master/src/anupam/acrylic/EasyPaint.java Any help would be very appreciated!


UPDATE:

I got java.lang.NullPointerException at this line:

View v = new MyView(getBaseContext());
v.setDrawingCacheEnabled(true);
Bitmap cachedBitmap = v.getDrawingCache();
Bitmap copyBitmap = cachedBitmap.copy(Bitmap.Config.RGB_565, true); // <--- HERE

Happy new year!

New2Android
  • 37
  • 1
  • 1
  • 8

1 Answers1

1

If you're assigning the View programmatically, then you already have it:

View view = new MyView(context);
setContentView(view);

Now you can draw contents of view into Bitmap and save it wherever you want (search for solutions, I don't think it's part of this question).

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
  • it's won't crash if we write it like this ? View view = new MyView(getBaseContext()); – New2Android Jan 03 '15 at 20:40
  • i used View view = new MyView(getBaseContext()); and View view = new MyView(this); and it's crashed in both cases,how i can solve that please? – New2Android Jan 03 '15 at 21:08
  • @New2Android please provide a stacktrace of crash soI I can see what's wrong. – Dmitry Zaytsev Jan 04 '15 at 14:17
  • @New2Android you did not initialized drawing cache before using it. As I said - search for examples of drawing views into bitmaps. – Dmitry Zaytsev Jan 04 '15 at 18:19
  • I'm bored with that, i'm thinking to take the mBitmap (check the link plz)), but the background would be black (empty) , how to set the canvas as background? – New2Android Jan 04 '15 at 18:51
  • @New2Android sorry, you're talking completely out of context - it's not clear what's your problem at the moment. – Dmitry Zaytsev Jan 04 '15 at 19:57
  • Can you help me with converting the MyView to bitmap please? I tried different solutions here but always i got a crash.. Thank you! – New2Android Jan 05 '15 at 17:02
  • @New2Android I'm using this approach which works pretty well: http://stackoverflow.com/a/3036736/926907 – Dmitry Zaytsev Jan 05 '15 at 17:13
  • View v = new MyView(getBaseContext()); Bitmap copyBitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(copyBitmap); Drawable bgDrawable =v.getBackground(); if (bgDrawable!=null) bgDrawable.draw(canvas); else canvas.drawColor(Color.WHITE); v.draw(canvas);copyBitmap.compress(CompressFormat.PNG, 100, output); itried both answers but it's crashs.. what's wrong with this please? – New2Android Jan 05 '15 at 17:30
  • @New2Android either create another question related to your particular problem, or include stacktrace of crash to your question. We're no magicians. – Dmitry Zaytsev Jan 05 '15 at 17:40
  • I give up with MyView, check my solution here http://stackoverflow.com/questions/27786623/draw-over-a-white-bitmap-another-bitmap Thank you for your help Dmitry. – New2Android Jan 05 '15 at 20:01