How I use the image of a screenshot? - android

I want to use the code of this link: How to take a screenshot and share it programmatically to take screenshots of my application. But I want the picture which is produced to use it to change my background, like this Layout.setBackgroundResource(resid).
How I can do this?
Where I will find the image's path to use it to change Background?

First you need the view you want a screen shot of:
View anyView = findViewById(R.id.anyView);
Then use this method to take a screenshot:
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
like this:
Bitmap screenshot = screenShot(anyView);
Bitmaps can't be set as a background image so lets convert it to a drawable:
Drawable drawableScreenshot = new BitmapDrawable(getResources(), screenshot);
Now set it to be the View background:
anyView.setBackgroundDrawable(drawableScreenshot);

Related

Add text into image and share as a img file android

I am trying to make an app like HBD wish sharing app but I want to add text to the image and share it as an image file. add text in the image in Relative Layout is simple but I want to share that image and text as an image file. my app maybe it looks like a certificate generator(sololearn app for example)
Thank You
Just pass the id of the relative layout in here and it will return a bitmap of that entire view. You can then save that as a PNG file and you're good to go.
private Bitmap getBitmapFromView(View view) {
view.setDrawingCacheEnabled(true);
Bitmap returnedBitmap = view.getDrawingCache();
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;
}

Take proper screenshot

I would like to ask help. I want to take screenshot. This is working, but ImageView rounded corners are not rounded on the taken screenshots (and elevation shadows missing too). I used
imageView.setClipToOutline(true);
to make this work on the phone, but on the screenshots they are rectangular.
These are my screenshot utils:
public class ScreenShotUtils {
public static Bitmap screenShotFromCanvas(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
public static Bitmap screenShotFromDrawingCache(View view) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.destroyDrawingCache();
view.setDrawingCacheEnabled(false);
return bitmap;
}
Both method has the same result.
However i noticed, that if i turn the hardware acceleration off, the layout looks like the same as on the screenshots.
This is how it looks like after take screenshot:
This is how it should be:
Finally i solved my problem with MediaProjectionManager.

How to take a screenshort of a view on android?

I want to take a picture of a view in Activity and share this picture.
First I use this method :
private Bitmap getShareViewShot() {
this.setDrawingCacheEnabled(true);
this.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(this.getDrawingCache(), 0, mSharePageRoot.getTop(), this.getWidth(), mSharePageRoot.getBottom());
this.setDrawingCacheEnabled(false);
return bitmap;
}
But if the view is in a ScrollView and the view has scrolled outside the screen, this method can't get a whole picture of this view.
So I change this method to below :
private Bitmap getShareViewShot() {
Bitmap bitmap = Bitmap.createBitmap(mSharePageRoot.getWidth(), mSharePageRoot.getHeight(),
Bitmap.Config.ARGB_8888);
mSharePageRoot.layout(0, 0, mSharePageRoot.getLayoutParams().width, mSharePageRoot.getLayoutParams().height);
final Canvas canvas = new Canvas(bitmap);
mSharePageRoot.draw(canvas);
return bitmap;
}
But another question comes: this view has no backgroud, so the picture I get has a black backgroud.In my code ,the Activity have a network image as the backgroud, so this means this method can't get the backgroud of the activity.
Please forgive me for my bad English.
Is there any other method ? thanks.
Yes, there is another method. There is a library by google called ASL(Android screenshot library). It's pretty simple.
https://code.google.com/archive/p/android-screenshot-library/wikis/DeveloperGuide.wiki
You can draw background by canvas, like this:
private Bitmap getShareViewShot() {
Bitmap bitmap = Bitmap.createBitmap(mSharePageRoot.getWidth(), mSharePageRoot.getHeight(),
Bitmap.Config.ARGB_8888);
mSharePageRoot.layout(0, 0, mSharePageRoot.getLayoutParams().width, mSharePageRoot.getLayoutParams().height);
final Canvas canvas = new Canvas(bitmap);
// the bitmap will have a white background
canvas.drawColor(0xffffffff);
mSharePageRoot.draw(canvas);
return bitmap;
}

How to take screenshot of layout with extra part as transparent not black in android

Current i m doing one image editing android app in which i need to take screenshot of one relative layout My code:
public Bitmap takeScreenshot() {
Bitmap bitTmp;
View rootView = findViewById(R.id.rl_img_view);
rootView.setDrawingCacheEnabled(true);
bitTmp = rootView.getDrawingCache();
return bitTmp;
}
my problem is that when i take screenshot it send me bitmap which contain empty part of layout as black color.
Is it possible to get that empty part transparent on output bitmap?
Output i get: http://postimg.org/image/5mxznc6jv
Output i want: http://postimg.org/image/50bre04kf
Change the width of the bitmap you are gettting, or maybe add padding from left.
Use this :
Public Bitmap pad(Bitmap Src, int padding_x, int padding_y){
Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Canvas can = new Canvas(outputimage);
can.drawARGB(FF,FF,FF,FF); //This represents White color
can.drawBitmap(Src, padding_x, padding_y, null);
return outputimage;
}
Refer to this question : Canvas Bitmap

How to get the whole bitmap attached to an ImageView?

I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.
I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.
Could I get the whole bitmap attached to a ImageView?
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
I think that's what you wanted.
If your drawble is not always an instanceof BitmapDrawable
Note: ImageView should be set before you do this.
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
Your bitmap is stored in bitmap.
Voila!
Easiest way is to set tag in ImageView.
imageView.setImageBitmap(bitmap);
imageView.setTag(bitmap);
To get Tag from it
imageView.getTag();

Categories

Resources