Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture whole scrollview bigger than screen [duplicate]

The main issue is saving the whole scroll view as a bitmap image rather than just what appears on the screen. Is there a way to save whole scroll view, and if so how?

like image 874
user3693635 Avatar asked Nov 11 '22 07:11

user3693635


1 Answers

Make a RelativeLayout or LinearLayout in your ScrollView to get Bitmap from Layout.

Organize this way:

public class ActivityA extends Activity {

LinearLayout PP_Ll;
Button btn_capture;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

PP_Ll = (RelativeLayout) findViewById(R.id.PP_Ll);
Button btn_capture= (Button) findViewById(R.id.btn_capture);

btn_capture.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        Bitmap bitmap = takeScreenshot();
        saveBitmap(bitmap);

        }
    });

public Bitmap takeScreenshot() {

    View rootView = getWindow().getDecorView().findViewById(R.id.PP_Ll);
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();

}

public void saveBitmap(Bitmap bitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File newDir = new File(root + "/Folder");
    newDir.mkdirs();
    Random gen = new Random();
    int n = 10000;
    n = gen.nextInt(n);
    String fotoname = "Photo-" + n + ".jpg";
    File file = new File(newDir, fotoname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        Toast.makeText(getApplicationContext(),
                "Saved in folder: 'Folder'", Toast.LENGTH_SHORT).show();

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

}
}}

Your RelativeLayout will be saved in certain directory as a BitMap. Good luck.

Note: You have to insert your imageviews, textviews etc. into your RelativeLayout in .xml file that all can go into screenshot.

like image 58
Umit Kaya Avatar answered Nov 15 '22 07:11

Umit Kaya