1

I'm trying to screenshot a ScrollView and simply copy+pasted the solution from here: Taking a "screenshot" of a specific layout in Android . Unfortunately I'm getting a NP @ Bitmap b = Bitmap.createBitmap(u.getDrawingCache());

My XML is

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollingProfile"
    android:layout_width="match_parent"
    android:fillViewport="true"
    android:layout_height="wrap_content"
    android:background="@drawable/tile_tan_repeat">

    <LinearLayout 
    android:id="@+id/paidLayoutLinearParent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
        <!--......-->
    </LinearLayout>
 </ScrollView>

Anyone have any ideas of why this is happening? I've never really worked with saving files like this so debugging this code is pretty unfamiliar to me.

Thank you!

Edit:This is the code I'm using

  public void onClick(View v) {
    if(v.getId()==R.id.screenshot){//source: https://stackoverflow.com/questions/10650049/taking-a-screenshot-of-a-specific-layout-in-android

        View u = findViewById(R.id.scrollingProfilePaid);
        u.setDrawingCacheEnabled(true);                                                
        ScrollView z = (ScrollView) findViewById(R.id.scrollingProfilePaid);
        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'profile.jpg'").format(new Date());
        String fileName = "profile.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(getContentResolver(), b, "Screen", "screen");
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }       
}
Community
  • 1
  • 1
James
  • 45
  • 5

1 Answers1

0

just put the code inside

scrollview.post(
new Runnable()
    {

        @Override
        public void run()
        {
         //this method will call after scrollview's onDraw method
         // so the view is ready to generarte bitmap at this point 
         //put here  your screen shot code

        }
     });
Ratul Ghosh
  • 1,512
  • 9
  • 15
  • Cannot instantiate the type Runnable ? – James Jul 19 '14 at 21:19
  • try this now , i was not in eclipse , sorry . – Ratul Ghosh Jul 19 '14 at 21:21
  • I put all my code in run() but the same error occurred. I appreciate your help. If this is to meant ensure that the scrollview is populated @ time of screenshot then that's not a problem. The screenshot is taken upon user click of a button – James Jul 19 '14 at 22:01