Take a screenshot of the entire list, even if all list items are not fully displayed on the screen
I would like to write some code to take a screenshot of a list even though it is not fully displayed on the screen. Can anyone help me on this issue.
Here is my code and when I try to run this app it says there is an exception on this line with a null pointer // int totalHeight = listView.getChildAt (0) .getHeight (); int totalWidth = listView.getChildAt (0) .getWidth (); Please help me to solve this problem.
Button screenShot;
public String TAG = "";
String[] values = new String[] { "1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15" };
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TAG = getClass().getName();
screenShot = (Button) findViewById(R.id.screenshot_button);
listView = (ListView) findViewById(R.id.listView);
int totalHeight = listView.getChildAt(0).getHeight();
int totalWidth = listView.getChildAt(0).getWidth();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.layout(0, 0, totalWidth, totalHeight);
screenShot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Inside on click",
Toast.LENGTH_SHORT).show();
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
});
}
public Bitmap takeScreenshot() {
Log.d(TAG, "Inside takeScreenShot method");
listView.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(listView.getDrawingCache());
// View rootView = findViewById(android.R.id.content).getRootView();
// rootView.setDrawingCacheEnabled(true);
return listView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory()
+ "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
}
source to share
Display display = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
int width = display.getWidth();
int height = "ParentLayoutOFView".getMeasuredHeight();
createImage(height, width, linearLayout, "FileName");
Add these methods
public File createImage(int height, int width, View view, String fileName) {
Bitmap bitmapCategory = getBitmapFromView(view, height, width);
return createFile(bitmapCategory, fileName);
}
public File createFile(Bitmap bitmap, String fileName) {
File externalStorage = Environment.getExternalStorageDirectory();
String sdcardPath = externalStorage.getAbsolutePath();
File reportImageFile = new File(sdcardPath + "/YourFolderName" + fileName + ".jpg");
try {
if (reportImageFile.isFile()) {
reportImageFile.delete();
}
if (reportImageFile.createNewFile()) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = new FileOutputStream(reportImageFile);
fo.write(bytes.toByteArray());
bytes.close();
fo.close();
return reportImageFile;
}
} catch (Exception e) {
Toast.makeText(ReportsActivity.this, "Unable to create Image.Try again", Toast.LENGTH_SHORT).show();
}
return null;
}
public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {
Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.measure(MeasureSpec.makeMeasureSpec(totalWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY));
view.layout(0, 0, totalWidth, totalHeight);
view.draw(canvas);
return returnedBitmap;
}
source to share
This is because when called onCreate
, your view has not been drawn yet, so you cannot get its height. Try adding this code at the end of your method onCreate
to wait for the ListView to be drawn and get the height:
listView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// You only need this method to be called once
listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Store the height in a class variable
mItemHeight = listView.getChildAt(0).getHeight()
}
});
EDIT: I missed .getViewTreeObserver()
source to share