Taking a screenshot of a specific layout in Android
Thanks to you guys, I've finally found out what was wrong.
View v = view.getRootView();
should not be used because it will call the root view which I do not want. I mistakenly thought this did not make a difference because I had entered the wrong resource ID.
MeasureSpec
somehow did not give a good account of the width and height. So I ended up using another method:
...
ScrollView z = (ScrollView) findViewById(R.id.scroll);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
...
As ScrollView's total height can be determined by the single child element that it has.
After making these changes, I am now able to take a screenshot of a nested ScrollView and all its contents, visible or not. For anyone interested, here is the block of code including the saving of the bitmap:
View u = findViewById(R.id.scroll);
u.setDrawingCacheEnabled(true);
ScrollView z = (ScrollView) findViewById(R.id.scroll);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
//Save bitmap
String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder";
String fileName = new SimpleDateFormat("yyyyMMddhhmm'_report.jpg'").format(new Date());
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
How to take screenshot of a layout in android
Have you got the
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Declared in your manifest?
Also if you want the entire view try this:
view1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view1.getDrawingCache());
Oh and this may save you some time:
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
Src: How to programmatically take a screenshot in Android?
Take a screenshot of a specific LinearLayout
Convert the view to Bitmap like this
Ref:Convert view to bitmap on Android
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;
}
Then use this Bitmap and save it in external like you have.
Taking screenshot of a particular layout
This is the problem:
View v1 = getWindow().getDecorView().getRootView();
You are taking the screenshot of the root view.
You have to place an ID on your RelativeLayout, which is the root of your activity layout, like this:
android:id="@+id/myview"
And then replace the first line I mentioned with:
View v1 = findViewById(R.id.myview);
You will get the bitmap for the layout, without the actionbar and the above blue bar (which is the background of the status bar).
Let me know if it works for you!
android- Taking screenshot of a particular view programmatically does not work
If the problem is getDrawingCache returning null, then just add this lines of code before v.buildDrawingCache(true);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
This will prevent that the view has a size of (0,0), and because of that it turns null safe.
Capture a particular layout Screenshot or save a particular layout in a different Activity Imageview
Capture screenshot of your layout on some event. use below code. and send it using intent to next activity.
yourlayout.setDrawingCacheEnabled(true);
Bitmap myBitmap = yourlayout.getDrawingCache();
Intent intent=new Intent(this,NextActivity.class);
intent.putExtra("data", myBitmap )
startActivity(intent);
Take a screenshot of a whole View
Actually I found the answer:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getWidth() , v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
Related Topics
Android Fragmenttransaction Custom Animation (Unknown Animator Name: Translate)
Rendering Problems in Android Studio V 1.1/1.2
Debugging Android Ndk Native Apps
How to Get the Current Dns Servers for Android
Start New Activity and Finish Current One in Android
Android Studio 3.0 Compile Issue (Cannot Choose Between Configurations)
How to Create Multiple Pendingintents with the Same Requestcode and Different Extras
How to Detect Wifi Tethering State
Android Gridview Keep Item Selected
How to Use Textview.Getlayout()? It Returns Null
Change Date String Format in Android
Page Scroll When Soft Keyboard Popped Up
Android Cardview Remove Padding
Cardview Inside Recyclerview Has Extra Margins
How to Limit Fling in Android Gallery to Just One Item Per Fling
What Is the Android UI Thread Stack Size Limit and How to Overcome It