How to print image displayed in ImageView in android Studio

Viewed 2946

I have a recyclerView which will display all images from JSON path and I used a click event that will display the clicked image to a new activity.

Now I want to print the image that is displayed. I have put a print button in the activity and while clicking on the print button i should get image to be printed in the network printer. I have used the below code. but I am getting error in getActivity() .

Button print = findViewById(R.id.print);
        //imageView.setDrawingCacheEnabled( true );


        print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PrintHelper photoPrinter = new PrintHelper(getActivity());
                photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
                //Bitmap bitmap = imageView.getDrawingCache(  );
                Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                photoPrinter.printBitmap("test print",bitmap);
            }
        });

Any help will be greatly appreciated :)

2 Answers

Use Activity.this instead of getActivity() where Activity is the name of your Activity.

The method getActivity() is used when you want to reference Fragments. But here you have an activity so there is an error! You have to do something like this:

//Other code
PrintHelper photoPrinter = new PrintHelper(YourJavaClassName.this);
//Other code

Please be sure to change YourJavaClassName to your activity's class name.

You can use getapplicationcontext() method or YourCurrentActivityName.this

Related