Android Get Screenshot of All Listview Items

Android get screenshot of all ListView items

working code:

public static Bitmap getWholeListViewItemsToBitmap() {

ListView listview = MyActivity.mFocusedListView;
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;
}

return bigbitmap;
}

When taking screenshot for whole layout only visible listitems on screen are coming in bitmap

Please go through below code.

private void shareViaPdfOrImage(String selection) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap, selection);
}

private void saveBitmap(Bitmap myBitmap, String selection) {
final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tallyandroid";

if (selection.equals("PDF")) {
createPdf(myBitmap, dirPath);
} else if (selection.equals("Image")) {
createImage(myBitmap, dirPath);
}
}

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics displaymetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float hight = displaymetrics.heightPixels;
float width = displaymetrics.widthPixels;

int convertHighet = (int) hight, convertWidth = (int) width;

PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(myBitmap.getWidth(), myBitmap.getHeight(), 1).create();
PdfDocument.Page page = document.startPage(pageInfo);

Canvas canvas = page.getCanvas();

Paint paint = new Paint();
paint.setColor(Color.parseColor("#ffffff"));
canvas.drawPaint(paint);

myBitmap = Bitmap.createScaledBitmap(myBitmap, myBitmap.getWidth(), myBitmap.getHeight(), true);

paint.setColor(Color.BLUE);
canvas.drawBitmap(myBitmap, 0, 0, null);
document.finishPage(page);

// write the document content
File dir = new File(dirPath);
if (!dir.exists())
dir.mkdirs();
// Long tsLong = System.currentTimeMillis() / 1000;

String fileName = "LastImageShared.pdf";
File filePath = new File(dirPath, fileName);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}

// close the document
document.close();

//sending broadcast to the system file manager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
final Uri contentUri = Uri.fromFile(filePath);
scanIntent.setData(contentUri);
sendBroadcast(scanIntent);
} else {
final Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
}

File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/tallyandroid", fileName);
sharePdf(outputFile);
}
private void shareImg(File dir) {
try {
Uri uri = FileProvider.getUriForFile(LedgerTransactionHistoryActivity.this, LedgerTransactionHistoryActivity.this.getApplicationContext().getPackageName() + ".provider", dir);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}

}

private void sharePdf(File dir) {
try {
progress_dialog.dismiss();
Uri uri = FileProvider.getUriForFile(LedgerTransactionHistoryActivity.this, LedgerTransactionHistoryActivity.this.getApplicationContext().getPackageName() + ".provider", dir);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("application/pdf");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
} catch (Exception e) {
Log.e("Error", e.getMessage());
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}

private Bitmap takeScreenshot() {

RelativeLayout mainView = findViewById(R.id.mainView);
int childcount = mainView.getChildCount();
Log.e("childCount", "==>" + childcount);
ArrayList<Bitmap> bitmaps = new ArrayList<>();
//bitmaps.add(getBitmapFromView(mainView));
height = 0;
for (int i = 0; i < childcount; i++) {
View subview = mainView.getChildAt(i);
if (subview.getId() != findViewById(R.id.empty_view).getId()) {
if (subview instanceof SwipeRefreshLayout) {
bitmaps.add(getListViewBitmap(listView));
} else {
bitmaps.add(getBitmapFromView(subview));
}
}
}

Bitmap bigbitmap = Bitmap.createBitmap(mainView.getMeasuredWidth(), height,
Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bigbitmap);
bigcanvas.drawColor(Color.WHITE);
Paint paint = new Paint();
int iHeight = 0;
for (int i = 0; i < bitmaps.size(); i++) {
Bitmap bmp = bitmaps.get(i);
bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
iHeight += bmp.getHeight();
bmp.recycle();
bmp = null;
}
return bigbitmap;
}

public Bitmap getListViewBitmap(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(
View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());

height = height + 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);
bigcanvas.drawColor(Color.WHITE);
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;
}
return bigbitmap;
}

public Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
height = height + view.getHeight();
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) bgDrawable.draw(canvas);
else canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}

Check out this code, if bitmaps are merging or not. In my contexts, it will work for sure.

capture listview screen

We need to capture the listview by capturing each and every listitem in the listview.
after capturing each and every item in the listview we need to add all the captured list items and make a bitmap image.
This way we can capture an listview even if all its items are not visible



Related Topics



Leave a reply



Submit