3

I simply need a Screenshot as an Bitmap of my ListView. I can't figure out how to do this. The bitmap of the screen ist than used to blur it and set is as an background in another Fragment. Where do i take Screenshot from? BaseAdapter or my Fragment which contains the ListView? or in the new Fragment that gets opened after clicking an Item in the ListView?

UPDATE: Im calling the method inside a ViewTreeObserver from my New Fragment. The Method loadBitmapFromView works perfectly. My Problem now is i don't know how to get hold of the ListView which i want the picture from. The Params i use mContainer,mContainer.getWidth(),mContainer.getHeight() should change to the one from my ListView. (The params right now are form the new Fragment for testing purposes)

private void applyBlur() {

    mContainer.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            mContainer.getViewTreeObserver().removeOnPreDrawListener(this);
            mContainer.buildDrawingCache();

            blur(MOKListViewFragment.loadBitmapFromView(mContainer,mContainer.getWidth(),mContainer.getHeight()), mContainer);
            return true;
        }
    });
}

This is how i call my new fragment from my BaseAdapter of the ListView which i actually want my picture from.

ImageView imageView = (ImageView) rowView.findViewById(R.id.thumb_button_1);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MOKPagerFragment pagerFragment = new MOKPagerFragment();
                    FragmentTransaction fragmentTransaction = ((Activity) mContext).getFragmentManager().beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
                    fragmentTransaction.add(R.id.fragment_container, pagerFragment);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                }
            });

Sorry if this is confusing.

Daniel Storch
  • 327
  • 3
  • 15
  • 1
    see this link http://stackoverflow.com/questions/12742343/android-get-screenshot-of-all-listview-items?rq=1 – Vaishali Sutariya Sep 26 '14 at 11:59
  • i think thats more than what i need. I don't need the screenshot of all items included the ones that are not showing. Simply need a screenshot of the current state of the ListView so i can blur that bitmap for background purposes. – Daniel Storch Sep 26 '14 at 12:10

2 Answers2

1

Take the screenshot from Fragment which contains the ListView, use this code to take the screenshot:

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;
}
Sagar Pilkhwal
  • 5,914
  • 2
  • 27
  • 78
  • Thanks, i will try it. How would i call this method from my New Fragment to get the Bitmap? (Sorry, im new to android and programming) – Daniel Storch Sep 26 '14 at 12:05
  • `loadBitmapFromView(View v, int width, int height)` takes 3 params, 1) View object(the view who's screenshot you want, here ListView), 2) width of the image, 3) height of the image. and if you want to take screenshot of the whole list view refer this [SO Post](http://stackoverflow.com/a/12956881/3326331) – Sagar Pilkhwal Sep 26 '14 at 12:08
  • somehow i get this error java.lang.IllegalArgumentException: width and height must be > 0. Not sure if im doing it right. – Daniel Storch Sep 26 '14 at 12:29
  • whats the width and height you are providing when calling the method ? can you edit your question and put the code where you are calling the `loadBitmapFromView()` method ? – Sagar Pilkhwal Sep 26 '14 at 12:32
  • The method worked. First i tried calling it in onCreatView from the new Fragment but realised that the View wasn't inflated yet and has no with or hight. I will edit the code with a new question if you don't mind. – Daniel Storch Sep 26 '14 at 12:44
  • If you have time, i would appreciate it if you answer my Edited post. Thanks – Daniel Storch Sep 26 '14 at 13:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61987/discussion-between-sagar-pilkhwal-and-daniel-storch). – Sagar Pilkhwal Sep 26 '14 at 13:09
0
if your phone is rooted try this

Process sh = Runtime.getRuntime().exec("su", null,null);

                OutputStream  os = sh.getOutputStream();
                os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
                os.flush();

                os.close();
                sh.waitFor();

then read img.png as bitmap and convert it jpg as follows

Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+   File.separator +"img.png");

now you can use screen bitmap

refrence from here

Community
  • 1
  • 1
Vaishali Sutariya
  • 5,103
  • 29
  • 32