Screenshot of a list view and converting it to a bitmap
I just need a screenshot as a bitmap of my ListView. I cannot figure out how to do this. The ist screen bitmap is used to blur it and set it as a background in another fragment. Where can I get a screenshot? BaseAdapter or my fragment that contains the ListView? or in a new snippet that opens after clicking an item in the ListView?
UPDATE: I am calling a method inside the ViewTreeObserver from my new fragment. The method loadBitmapFromView
works fine. My problem now is that I don't know how to access the ListView from which I want to get the image. The params I'm using mContainer,mContainer.getWidth(),mContainer.getHeight()
should change to one of my ListView. (Parameres are a new snippet right now 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 snippet from my BaseAdapter ListView that I really want my image to be.
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.
source to share
Take a screenshot from Fragment which contains the ListView
, use this code to take a 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;
}
source to share
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 to jpg like this
Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+ File.separator +"img.png");
now you can use screen bitmap
refrence from here
source to share