0

If your activity has too much content and display a very long ui, like containing a ListView or WebView,

How can we get a screenshot containing the whole content within a long image?

Waylan Punch
  • 239
  • 4
  • 17
  • Maybe I wasn't clear so someone downvote this question, @Amir Ziarati 's answer is quite well. And I will flag this question as **duplicate** – Waylan Punch Jan 03 '17 at 07:48
  • Possible duplicate of [How to convert all content in a scrollview to a bitmap?](http://stackoverflow.com/questions/8738448/how-to-convert-all-content-in-a-scrollview-to-a-bitmap) – Waylan Punch Jan 03 '17 at 07:49

3 Answers3

1

I think the answer in this link can help you:

How to convert all content in a scrollview to a bitmap?

I mixed two answers in the link above to make an optimized piece of code for you:

private void takeScreenShot() 
{
View u = ((Activity) mContext).findViewById(R.id.scroll);

HorizontalScrollView z = (HorizontalScrollView) ((Activity) mContext).findViewById(R.id.scroll);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();

Bitmap b = getBitmapFromView(u,totalHeight,totalWidth);             

//Save bitmap
String extr = Environment.getExternalStorageDirectory()+"/Folder/";
String fileName = "report.jpg";
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myPath);
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
    MediaStore.Images.Media.insertImage(mContext.getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

public static Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {

int height = Math.min(MAX_HEIGHT, totalHeight);
float percent = height / (float)totalHeight;

Bitmap canvasBitmap = Bitmap.createBitmap((int)(totalWidth*percent),(int)(totalHeight*percent), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(canvasBitmap);

Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
   bgDrawable.draw(canvas);
else
   canvas.drawColor(Color.WHITE);

canvas.save();
canvas.scale(percent, percent);
view.draw(canvas);
canvas.restore();

return canvasBitmap;
}
Community
  • 1
  • 1
Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
1

Try using this, Pass your view to this function,

public Bitmap getBitmapFromView(View view) {
        // Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
        // Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        // Get the view's background
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            // has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else
            // does not have background drawable, then draw white background on
            // the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        view.draw(canvas);
        // return the bitmap
        return returnedBitmap;
    }

This function will return a bitmap, you can utilize it the way you want.

Deep Patel
  • 2,584
  • 2
  • 15
  • 28
0

You can easily convert layout into "bitmap image"

protected Bitmap ConvertToBitmap(LinearLayout layout) {

    layout.setDrawingCacheEnabled(true);

    layout.buildDrawingCache();

    Bitmap bitmap = layout.getDrawingCache();

    return bitmap;

}
pavan jindam
  • 21
  • 1
  • 4