2

I have a LinearLayout and a RecyclerView just below it. Searching on Google, I found some code to take a screenshot of RecyclerView (To be true, I couldn't understand how it works). Here's the code:

  public static Bitmap getRecyclerViewScreenshot(RecyclerView view) {
        int size = view.getAdapter().getItemCount();
        RecyclerView.ViewHolder holder = view.getAdapter().createViewHolder(view, 0);
        view.getAdapter().onBindViewHolder(holder, 0);
        holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
        Bitmap bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), holder.itemView.getMeasuredHeight() * size,
                Bitmap.Config.ARGB_8888);
        Canvas bigCanvas = new Canvas(bigBitmap);
        bigCanvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        int iHeight = 0;
        holder.itemView.setDrawingCacheEnabled(true);
        holder.itemView.buildDrawingCache();
        bigCanvas.drawBitmap(holder.itemView.getDrawingCache(), 0f, iHeight, paint);
        holder.itemView.setDrawingCacheEnabled(false);
        holder.itemView.destroyDrawingCache();
        iHeight += holder.itemView.getMeasuredHeight();
        try {
            for (int i = 1; i < size; i++) {
                view.getAdapter().onBindViewHolder(holder, i);
                holder.itemView.setDrawingCacheEnabled(true);
                holder.itemView.buildDrawingCache();
                bigCanvas.drawBitmap(holder.itemView.getDrawingCache(), 0f, iHeight, paint);
                iHeight += holder.itemView.getMeasuredHeight();
                holder.itemView.setDrawingCacheEnabled(false);
                holder.itemView.destroyDrawingCache();
            }
        } catch (Exception e) {
        }

        return bigBitmap;
    }

Now I want to include the LinearLayout in it too which is just above the RecyclerView. Since I couldn't understand the code, I can't modify it to include LinearLayout. What I couldn't understand are the terms related to Bitmap like Canvas, DrawingCache. So If anyone can give some basic info, that would be great. Also help me include linearlayout in the processed Bitmap.

Paras Sidhu
  • 652
  • 1
  • 12
  • 36

1 Answers1

1

you can take it easily, you wouldn't need to code in adapter class it will be done in activity class and also you can take screenshot of scroll view type activity.just you have to find the id of layout in which RecycleView is declare in XML.i'm posting full activity you just find your code init, this code work for recycleview as well as nested scroll view

 @Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_download:

            Bitmap bitmap1 = getBitmapFromView(ll_linear);
            Log.e("ll_linear", "" + ll_linear.getWidth());
            Log.e("ll_linear", "" + ll_linear.getHeight());
            saveBitmap(bitmap1);
            break;

    }

}

public void saveBitmap(Bitmap bitmap) {
    isStoragePermissionGranted(bitmap);
}

public boolean isStoragePermissionGranted(Bitmap bitmap) {

    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

    Log.e("mpath", mPath);

    File imagePath = new File(mPath);
    FileOutputStream fos;

    if (Build.VERSION.SDK_INT >= 23) {

        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {

            try {
                fos = new FileOutputStream(imagePath);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.flush();
                fos.close();

                Toast.makeText(getApplicationContext(), imagePath.getAbsolutePath() + "", Toast.LENGTH_SHORT).show();
                boolean_save = true;
                btn_download.setText("Check image");

            } catch (FileNotFoundException e) {
                Log.e("Exception", "" + e);

            } catch (IOException e) {
                Log.e("Exception", "" + e);
            }
            Log.v("Tag", "Permission is granted");

            return true;

        } else {

            Log.v("Tag", "Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    } else { //permission is automatically granted on sdk<23 upon installation

        File imagePath1 = new File("/sdcard/screenshotdemo.jpg");
        FileOutputStream fos1;

        try {
            fos1 = new FileOutputStream(imagePath1);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos1);
            fos1.flush();
            fos1.close();
            Toast.makeText(getApplicationContext(), imagePath1.getAbsolutePath() + "", Toast.LENGTH_SHORT).show();
            boolean_save = true;
            btn_download.setText("Check image");

        } catch (FileNotFoundException e) {
            Log.e("Exception", "" + e);

        } catch (IOException e) {
            Log.e("Exception", "" + e);
        }
        Log.v("Tag", "Permission is granted");

        return true;
    }

}
 public static Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    //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;
}
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105