By using this demo you can take the screenshot of particular layout even if the layout have the scrollview. The screenshot is saved in your directory and you can easily load it from the file path using glide library. In this demo I have also added the run time permissions so this will also work on marshmallow and above versions Download source code from here.

Video Demo:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="deepshikha.com.screenshot">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">
 <activity android:name=".MainActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>

<activity android:name=".Screenshot"></activity>
 </application>

</manifest>


activity_main.xml

<LinearLayout android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:background="#efefef"
 xmlns:android="http://schemas.android.com/apk/res/android">


 <Button
 android:layout_width="match_parent"
 android:layout_margin="10dp"
 android:layout_height="50dp"
 android:id="@+id/btn_screenshot"
 android:text="Take ScreenShot"/>


 <ScrollView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/scrollView"
 android:layout_marginBottom="10dp"
 android:background="#ffffff">

<LinearLayout
 android:layout_width="match_parent"
 android:layout_marginLeft="10dp"
 android:id="@+id/ll_linear"
 android:layout_marginRight="10dp"
 android:orientation="vertical"
 android:layout_height="match_parent">


 <ImageView
 android:layout_width="match_parent"
 android:layout_marginTop="10dp"
 android:scaleType="fitXY"
 android:layout_height="200dp"
 android:layout_gravity="center"
 android:src="@drawable/image2"/>
 <ImageView
 android:layout_width="match_parent"
 android:layout_marginTop="10dp"
 android:scaleType="fitXY"
 android:layout_height="200dp"
 android:layout_gravity="center"
 android:src="@drawable/image3"/>

<ImageView
 android:layout_width="match_parent"
 android:layout_marginTop="10dp"
 android:scaleType="fitXY"
 android:layout_height="200dp"
 android:layout_gravity="center"
 android:src="@drawable/image5"/>


 <ImageView
 android:layout_width="match_parent"
 android:layout_marginTop="10dp"
 android:scaleType="fitXY"
 android:layout_height="200dp"
 android:layout_gravity="center"
 android:src="@drawable/image6"/>

</LinearLayout>


 </ScrollView>

 </LinearLayout>

MainActivity.xml

package deepshikha.com.screenshot;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

Button btn_screenshot;
 ScrollView scrollView;
 LinearLayout ll_linear;
 public static int REQUEST_PERMISSIONS = 1;
 boolean boolean_permission;
 boolean boolean_save;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 init();
 fn_permission();
 }

private void init() {
 btn_screenshot = (Button) findViewById(R.id.btn_screenshot);
 scrollView = (ScrollView)findViewById(R.id.scrollView);
 ll_linear = (LinearLayout) findViewById(R.id.ll_linear);

btn_screenshot.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {

if (boolean_save) {
 Intent intent = new Intent(getApplicationContext(),Screenshot.class);
 startActivity(intent);

}else {
 if (boolean_permission) {
 Bitmap bitmap1 = loadBitmapFromView(ll_linear, ll_linear.getWidth(), ll_linear.getHeight());
 saveBitmap(bitmap1);
 } else {

}
 }

}
 });
 }
public void saveBitmap(Bitmap bitmap) {

File imagePath = new File("/sdcard/screenshotdemo.jpg");
 FileOutputStream fos;
 try {
 fos = new FileOutputStream(imagePath);
 bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
 fos.flush();
 fos.close();
 Toast.makeText(getApplicationContext(),imagePath.getAbsolutePath()+"",Toast.LENGTH_SHORT).show();
 boolean_save = true;

btn_screenshot.setText("Check image");

Log.e("ImageSave", "Saveimage");
 } catch (FileNotFoundException e) {
 Log.e("GREC", e.getMessage(), e);
 } catch (IOException e) {
 Log.e("GREC", e.getMessage(), e);
 }
 }

public static Bitmap loadBitmapFromView(View v, int width, int height) {
 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
 Canvas c = new Canvas(b);
 v.draw(c);

return b;
 }

private void fn_permission() {
 if ((ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)||
 (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {

if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE))) {
 } else {
 ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
 REQUEST_PERMISSIONS);

}

if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE))) {
 } else {
 ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
 REQUEST_PERMISSIONS);

}
 } else {
 boolean_permission = true;


 }
 }


 @Override
 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 if (requestCode == REQUEST_PERMISSIONS) {

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

boolean_permission = true;


 } else {
 Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();

}
 }
 }
}

activity_screenshot.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_margin="10dp"
 android:orientation="vertical">

 <TextView
 android:id="@+id/tv_file"
 android:layout_gravity="center_horizontal"
 android:textColor="#000000"
 android:text="/sdcard/screenshotdemo.jpg"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

 <ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_marginTop="10dp"
 android:id="@+id/iv_image"/>


 </LinearLayout>
</ScrollView>

Screenshot.java

package deepshikha.com.screenshot;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

/**
 * Created by deepshikha on 8/5/17.
 */

public class Screenshot extends Activity {
 ImageView iv_image;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_screenshot);
 init();
 }

private void init() {
 iv_image = (ImageView) findViewById(R.id.iv_image);
 String completePath = Environment.getExternalStorageDirectory() + "/" + "screenshotdemo.jpg";
 Glide.with(Screenshot.this).load(completePath).error(R.drawable.image1).into(iv_image);

}


}
Advertisements