Tuesday, October 25, 2016

Convert Layout View to Image and Store in Storage (Android)

This code able to convert the whole view in scrollview to images. It been tested and successfully working.



 

First image is from the mobile, after click the "save and print receipt" it will save the whole view and stored in image gallery. Second image is the result



Change Snippet Background Color
 
        

@BindView(R.id.native_resit)
protected ScrollView native_resit;


@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ....
        ....

        close_btn.setOnClickListener(new resitClickListener());
        print_resit.setOnClickListener(new resitClickListener());

        runRecieptData();
}

private class resitClickListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.close_btn){
                getFragmentManager().popBackStack();
            }
            else if (view.getId() == R.id.print_resit){
                print();
            }
        }
}

private void print(){
        ProgressDialog dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Saving...");
        dialog.show();

        Bitmap bitmap = getBitmapFromView(native_resit,native_resit.getChildAt(0).getHeight(),native_resit.getChildAt(0).getWidth());
        try {
            File defaultFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Your_Folder");
            if (!defaultFile.exists())
                defaultFile.mkdirs();

            String filename = "Order ID "+orderHistoryResponse.getOrderId()+".jpg";
            File file = new File(defaultFile,filename);
            if (file.exists()) {
                file.delete();
                file = new File(defaultFile,filename);
            }

            FileOutputStream output = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
            output.flush();
            output.close();

            dialog.dismiss();

            Toast.makeText(getActivity(), Message.RECEIPT_SAVE, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            dialog.dismiss();
            Toast.makeText(getActivity(), Message.RECEIPT_SAVE_FAILED, Toast.LENGTH_SHORT).show();
        }
    }

    //create bitmap from the view
    private Bitmap getBitmapFromView(View view,int height,int width) {
        Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return bitmap;
    } 

 

2 comments:

  1. True, when it comes to test automation frameworks, there are many different types of frameworks used in today's applications and websites, such as data-driven, keyword-driven, hybrid, etc. Each has demand in the freelancing world; you can sign up with Eiliana.com- a global freelancing portal, and find top automation projects that match your domain.


    ReplyDelete