Friday, 24 April 2015

Capture the whole ListView and create an image ,Even if the whole part of it is not visible on the screen.

Hello Friends,

          Today i have done some new research on how can we capture an Image of the whole ListView even the whole items of listview are not visible on screen and save it as a bitmap even though all the items of the ListView are not visible on the screen.

I have made a simple method which gives you image of the listview.

So here i am going to show you the code to capture the listview and save it as an image into the SDcard.

You can use the below code in your application directly.
Here is the method which captures your whole ListView and convert it into the Bitmap.

  /**  
  * Take a screenshot of a whole listview even if the whole listview elements  
  * aren't fully visible in the screen  
  *   
  * @param p_ListView  
  *      -ListView instance  
  * @return-Bitmap of List  
  */  
  public void getWholeListViewItemsToBitmap(ListView p_ListView) {  
   ListView listview = p_ListView;  
  ListAdapter adapter = listview.getAdapter();  
  int itemscount = adapter.getCount();  
  int allitemsheight = 0;  
  List<Bitmap> bmps = new ArrayList<Bitmap>();  
   for (int i = 0; i < itemscount; i++) {  
   View childView = adapter.getView(i, null, listview);  
   childView.measure(  
    MeasureSpec.makeMeasureSpec(listview.getWidth(), MeasureSpec.EXACTLY),  
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
   childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());  
   childView.setDrawingCacheEnabled(true);  
   childView.buildDrawingCache();  
   bmps.add(childView.getDrawingCache());  
   allitemsheight += childView.getMeasuredHeight();  
  }  
   Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight,  
   Bitmap.Config.ARGB_8888);  
  Canvas bigcanvas = new Canvas(bigbitmap);  
   Paint paint = new Paint();  
  int iHeight = 0;  
   for (int i = 0; i < bmps.size(); i++) {  
   Bitmap bmp = bmps.get(i);  
   bigcanvas.drawBitmap(bmp, 0, iHeight, paint);  
   iHeight += bmp.getHeight();  
   bmp.recycle();  
   bmp = null;  
  }  
  storeImage(bigbitmap, "Test.jpg");  
  }  
  /**  
  * Convert the bitmap into image and save it into the sdcard.  
  *   
  * @param imageData  
  *      -Bitmap image.  
  * @param filename  
  *      -Name of the image.  
  * @return  
  */  
  public boolean storeImage(Bitmap imageData, String filename) {  
  // get path to external storage (SD card)  
  File sdIconStorageDir = new File(Environment.getExternalStorageDirectory()  
   .getAbsolutePath() + "/myAppDir/");  
   // create storage directories, if they don't exist  
  sdIconStorageDir.mkdirs();  
   try {  
   String filePath = sdIconStorageDir.toString() + File.separator + filename;  
   FileOutputStream fileOutputStream = new FileOutputStream(filePath);  
   BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);  
   Toast.makeText(m_cont, "Image Saved at----" + filePath, Toast.LENGTH_LONG).show();  
   // choose another format if PNG doesn't suit you  
   imageData.compress(CompressFormat.PNG, 100, bos);  
   bos.flush();  
   bos.close();  
   } catch (FileNotFoundException e) {  
   Log.w("TAG", "Error saving image file: " + e.getMessage());  
   return false;  
  } catch (IOException e) {  
   Log.w("TAG", "Error saving image file: " + e.getMessage());  
   return false;  
  }  
   return true;  
  }  

Here is the activity code which shows how you can use the above methods in your application.
I have called a method onClick of Button to generate image.
 public class SimpleListViewActivity extends Activity {  
  private ListView mainListView;  
  private ArrayAdapter<String> listAdapter;  
  /** Called when the activity is first created. */  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main);  
   // Find the ListView resource.  
  mainListView = (ListView) findViewById(R.id.mainListView);  
   // Create and populate a List of planet names.  
  String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn",  
   "Uranus", "Neptune", "Solar", "Pluto","Ceres","Haumea","Makemake","Eris" };  
  ArrayList<String> planetList = new ArrayList<String>();  
  planetList.addAll(Arrays.asList(planets));  
   // Create ArrayAdapter using the planet list.  
  listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);  
   // Set the ArrayAdapter as the ListView's adapter.  
  mainListView.setAdapter(listAdapter);  
  findViewById(R.id.button1).setOnClickListener(new OnClickListener() {  
   @Override  
   public void onClick(View v) {  
   getWholeListViewItemsToBitmap(mainListView);  
   }  
  });  
  }  

Layout file of an activity
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical" >  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Capture Screen"  
     android:visibility="visible" />  
   <ListView  
     android:id="@+id/mainListView"  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent"  
     android:background="@android:color/white"  
     android:listSelector="@null" >  
   </ListView>  
 </LinearLayout>  

Let me know if anyone facing any problem or any kind of improvement needed.

Thank you. 
Enjoy !!!




No comments:

Post a Comment