Android 2.1 View's getDrawingCache() method always returns null
Asked Answered
H

7

13

I'm working with Android 2.1 and have the following problem: Using the method View.getDrawingCache() always returns null. getDrawingCache() should return a Bitmap, which is the presentation of View's content.

Example code:

public void onCreate(final Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  final View view = findViewById(R.id.ImageView01);
  view.setDrawingCacheEnabled(true);
  view.buildDrawingCache();
  final Bitmap bmp = view.getDrawingCache();
  System.out.println(bmp);

}

I've already tried different ways to configure the View object for generating the drawing cache (e.g. View.setWillNotDraw(boolean) and View.setWillNotCacheDrawing(boolean)), but nothing works.

What is the right way, or what I'm doing wrong?

PS: In real code I want to apply getDrawingCache() on a ViewGroup like RelativeLayout. Is the behaviour the same when using a ViewGroup?

Hautboy answered 12/5, 2010 at 8:41 Comment(2)
I get the same problem in 2.1, 2.2 saves the bitmap properly.Fukuoka
This should work - #11561382Argyres
E
18
Bitmap screenshot;
view.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

You need Bitmap.createBitmap(view.getDrawingCache()); as the Bitmap from which the reference is received by getDrawingCache() gets recycled.

Effective answered 18/1, 2011 at 3:0 Comment(1)
Doesn't work for me! Says: source is null at Bitmap.createBitmap()Pilgrim
M
5

use

onLayout(x, y, width, height);

for ex:

onLayout(0, 0, 100, 100);

before calling getDrawingCache

Melson answered 12/5, 2010 at 15:33 Comment(3)
Doesn't work either: The method onLayout(boolean, int, int, int, int) from the type LinearLayout is not visiblePilgrim
its just: layout.layout(0, 0, wt, ht);Pilgrim
its strange but work for me ..(it drawn only parent view not it child view )Kristopherkristos
G
2

Never do these things in onCreate! You can do it in a View.onClickListener or other place.

Gorga answered 9/6, 2011 at 5:48 Comment(0)
T
2

I know this doesn't answer your question, but....

Just as an aside for other developers reading this, if what you are really trying to do is have access to a custom view's contents, derive your custom view from an ImageView. And then create a canvas and bitmap that you are going to control/draw into/read and use setImageBitmap() to set your bitmap into the view object. From there on in you can ignore the canvas passed to you in the onDraw(canvas) method and just draw into your local canvas/bitmap instead.

Why would you need this? There is an oversight in the Canvas class in my opinion. The Canvas class gives you no way to read the canvas contents nor to get at the bitmap behind it. So if you ever need to read from a bitmap, for example to acquire an image dump of the view contents, there's no way I know of to do it, other than the method described above.

This won't work for the RelativeView problem originally described here in this thread but this might be of interest to new Android developers.

Truelove answered 19/7, 2012 at 21:38 Comment(0)
N
0

As far as I read the last days in the documentation you should onlycall setDrawingCacheEnabled or buildDrawingChache() but not both.

Nikolai answered 12/5, 2010 at 8:53 Comment(1)
I know, that setDrawingCacheEnabled and buildDrawingChache is redundant. setDrawingCacheEnabled forces to build the drawing cache on demand and buildDrawingChache creates the cache without setting the the enable-flag. but it doesn't work for me - neither only setting the flag, nor the using the explicit call buildDrawingChache without setting the flag.Hautboy
P
0

had the same problem and finally this partly works for me:

view.layout(0, 0, 1000, 100);
Bitmap b = view.getDrawingCache();
try {
    b.compress(CompressFormat.JPEG, 95,
    new FileOutputStream( Environment.getExternalStorageDirectory()                                                            + "/folder/name.jpg"));
catch (FileNotFoundException e) {
    Log.e(LOGTAG, "error:" + e.getMessage());
    }

the only problem is, that I can't get the full dimensions of my view. view.getwidth returns null ...

Pilgrim answered 9/10, 2011 at 10:39 Comment(0)
F
0

Check out if your view is not bigger than the screen size. I found this and switched the dimensions of my view so, now it works =) Android get full View as Bitmap

Fukuoka answered 7/12, 2011 at 22:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.