Get Android view as Bitmap to save it on SD - android

I'm trying to save the path that is being drawn on a View but I just can't figure out how to do this.
Here is what I'm doing in my Activity to create the View and set it as content:
View accPathView = new AccPathView(this, steps);
setContentView(accPathView);
Then in the onDraw method of my View class, I'm simply creating a path and drawing it on the canvas I received as parameter:
But then, when I try to get the bitmap of my view with getDrawingCache(), it's always null and creates an empty image on my SD. I tried
accPathView.setDrawingCacheEnabled(true);
accPathView.buildDrawingCache(true);
Unfortunately it didn't change anything and I still get an empty bitmap.

You may try this:
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
EDIT:
How do you the above method?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View accPathView = new AccPathView(this, steps);
setContentView(accPathView);
accPathView.post(new Runnable() {
public void run() {
Bitmap viewBitmap = getBitmapFromView(accPathView);
}
});
// your remaining oncreate
}

Related

Create widget with customview

I'd like to use several CustomViews in my appwidget and thus want to create a bitmap out of every CustomView.
So i tried that with
Bitmap customBitmap = customView.getDrawingCache(true);
remoteViews.setImageViewBitmap(R.id.imageView1, customBitmap );
but it doesn't work at all.
Are there any suggestions?
yes, it is drawn on a canvas
I would do something like this:
public Bitmap createCustomView(){
Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// draw on the canvas:
// ...
return bitmap;
}
and then set the Bitmap to the ImageView:
remoteViews.setImageViewBitmap(R.id.imageView1, createCustomView() );

set bitmap which was created from RelativeLayout to imageView

I have RelativeLayout and I want to convert it to the Bitmap like this:
public static Bitmap getBitmapFromView(View view) {
if (view != null)
{ Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
else
return null;
}
then I create RelativeLayout cView in the onCreate() method dynamically and set it to the ImageView like this:
dialer = (ImageView) findViewById(R.id.imageView_ring);
dialer.setImageBitmap(getBitmapFromView(cView));
and get error:
05-20 13:48:27.509: E/AndroidRuntime(28367):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{ru.biovamp.widget/ru.biovamp.widget.TestCircleAct}:
java.lang.IllegalArgumentException: width and height must be > 0
I also tried to add bitmap to the ImageView from the onStart() method, but got the same result. What solution can I use?
I've solved my issue like this:
public Bitmap getBitmapFromView() {
this.measure(MeasureSpec.makeMeasureSpec(createNewRelativeLayoutParams().width, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(createNewRelativeLayoutParams().height, MeasureSpec.UNSPECIFIED));
this.layout(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
Bitmap returnedBitmap = Bitmap.createBitmap(this.getMeasuredWidth(), this.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(returnedBitmap);
this.draw(c);
return returnedBitmap;
}
so solution is that we should to call measure() and layout() methods before drawing it.
oficial explaniton

Create bitmap from view makes view disappear, how to get view canvas?

I found two ways of creating a Bitmap from a view. But once I do that, the view disappears and I can't use it anymore. How can I redraw the view after generating my bitmap?
1st:
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
2nd:
Bitmap viewCapture = null;
theViewYouWantToCapture.setDrawingCacheEnabled(true);
viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());
theViewYouWantToCapture.setDrawingCacheEnabled(false);
EDIT
So, I think I understand what happens on the first one, we are basically removing the view from it's original canvas and drawing it somewhere else associated with that bitmap. Can somehow we store the original canvas and then set the view to be redrawn there?
Sorry, I'm not immensely knowledgeable on this. But I use the following code:
public Bitmap getBitmapFromView(View view, int width, int height) {
Bitmap returnedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (view==mainPage.boardView) {
canvas.drawColor(BoardView.BOARD_BG_COLOR);
} else if (bgDrawable!=null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
view.layout(0, 0, width, height);
view.draw(canvas);
return returnedBitmap;
}
which is so similar to yours I suspect we copy&edited from the same place.
I have no trouble with the view disappearing from the original drawing tree. Mine is called for ViewGroups rather than plain Views.
Try this.
Getting the bitmap:
// Prepping.
boolean oldWillNotCacheDrawing = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
view.setDrawingCacheEnabled(true);
// Getting the bitmap
Bitmap bmp = view.getDrawingCache();
And make sure to reset the view back to its old self.
view.destroyDrawingCache();
view.setDrawingCacheEnabled(false);
view.setWillNotCacheDrawing(oldWillNotCacheDrawing);
return bmp;
Guy's answer works when the View has not yet been laid out in a parent view.
If the view already has been measured and laid out in a parent-view, Guy's answer may screw up your Activity's layout.
If the view hasn't yet been measured and laid out, Guy's answer works fine.
My answer would work after the View has been laid out and it won't screw up the Activity's layout since it doesn't measure and layout the View again.

Screenshot of the visible part of the page in WebView

In my application is a WebView widget, which opens a big page.
How do I capture the visible part of the page in WebView? capturePicture() is not suitable for it...
public static Bitmap getBitmapForVisibleRegion(WebView webview) {
Bitmap returnedBitmap = null;
webview.setDrawingCacheEnabled(true);
returnBitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
return returnedBitmap;
}
Save your webView under bitmap. Then you save bitmap on SD card.
Use this to get your bitmap from webView :
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.TRANSPARENT);
view.draw(canvas);
return returnedBitmap;
}
Now just save it on SD and it's done

Posting ScrollView Content on FaceBook wall

I used ScrollView in my application. It contains a LinearLayout as child and some other views likes
TextViews,Buttons etc... as sub-views. Now I want to post the content of scrollview as an image on FaceBook wall.I could not find out a suitable post.
**My question is how to post ScrollView/Layout content on facebook wall?...**
Thanks in Advance
use this function to convert your view in to bitmap image
public Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(480, 640,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
then post bitmap image on facebook wall
hope help you..
I changed above code to
public Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight() , Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
It works fine and got bitmap of all content.

Categories

Resources