Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert all content in a ScrollView to a bitmap?

I try to take a convert my ScrollView into bitmap, my ScrollView has content that spills out of the screen (hence scrolling made possible), after I Ask here I can capture my Activity in ScrollView, But I got A problem, the Bitmap for saving a Screenshot not create All View, only the last of ScrollView, and the rest of it is Black screen as Below : enter image description here

this is my code for taking a Screenshot for all view in ScrollView:

 scroll_pp=(ScrollView)polis.findViewById(R.id.scroll_pp);
   utktest=polis.findViewById(R.id.scroll_pp);             
   int totalHeight = scroll_pp.getChildAt(0).getHeight();
   int totalWidth = scroll_pp.getChildAt(0).getWidth();
   Bitmap b= MethodSupport.loadBitmapFromView(utktest, totalWidth, totalHeight);

 String extr = Environment.getExternalStorageDirectory().toString() +   File.separator + "Folder";
                    String fileName = "Test.jpg";
                    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(getActivity().getContentResolver(), b, "Screen", "screen");
                    }catch (FileNotFoundException e) {

                        e.printStackTrace();
                    } 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.draw(c);
        return b;
    }

So Is there something wrong with my code so I can not display the bitmap in according what I want?

like image 223
Menma Avatar asked Sep 30 '22 00:09

Menma


1 Answers

You need to get First children of ScrollView and convert it to Bitmap.

Example:

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

    <RelativeLayout
        android:id="@+id/rlChildContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    </RelativeLayout>
</ScrollView>

You should try to load bitmap only when screen is loaded and layout is drawn, don't do it from onCreate method of Activity.

like image 64
Mehul Joisar Avatar answered Oct 13 '22 00:10

Mehul Joisar