jomin v george
jomin v george

Reputation: 1359

How to make an image of a complete scrolling view in android

How to take screenshot of a complete scrolling view in android? I found a lot of questions in stackoverflow but nothing solved my problem.

private void SaveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete ();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);
    return b;
}

This is the code am finally used. it's giving active screen's image with other area as dark. When using this android make screenshot of entire layout

xml layout file content

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:id="@+id/rootofall"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">



    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <ScrollView
        android:layout_below="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView"
        >
    <TextView android:text="@string/alotofdata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/button"
        android:id="@+id/textView" />

    </ScrollView>
</RelativeLayout>

Upvotes: 4

Views: 1661

Answers (1)

Arth Tilva
Arth Tilva

Reputation: 2496

Try passing scrollview to take bitmap. This might help you. I am getting bitmap of whole view. Need to use this code with Handler or in layoutObserver, because this will be only effective after the layout gets inflated. I have tried using Handler with post delayed of 2s.

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
Bitmap bitmap = Bitmap.createBitmap(
scrollView.getChildAt(0).getWidth(),
scrollView.getChildAt(0).getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
scrollView.getChildAt(0).draw(c);

//Do whatever you want with your bitmap

saveBitmap(bitmap);

Upvotes: 3

Related Questions