0

i created screenshot for my activity in programatically.it work perfectly.i have problem in after taking shot the image showing in my activity.how to hide it?

Mycode :

public class MainActivity extends AppCompatActivity {

    File cacheDir;
    final Context context = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button print = (Button) findViewById(R.id.btn_print);

        print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                takeScreenShot();
            }
        });



    }

       private void takeScreenShot() {

        View u =  findViewById(R.id.activity_main);

        int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        u.measure(spec, spec);
        u.layout(0, 0, u.getMeasuredWidth(), u.getMeasuredHeight());

        Bitmap b = getBitmapFromView(u,u.getMeasuredHeight(),u.getMeasuredWidth());

        final String root = Environment.getExternalStorageDirectory().toString();

        File myPath = new File(root + "/saved_img");
        myPath.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+n+".jpg";
        File file = new File(myPath, fname);
        FileOutputStream fos = null;
        if(file.exists()) file.delete();
        try{
            fos = new FileOutputStream(file);
            b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();


        } catch(FileNotFoundException e){
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }

        Toast.makeText(this,"screen captured",Toast.LENGTH_SHORT).show();
    }



    public Bitmap getBitmapFromView(View u, int totalHeight, int totalWidth){
        Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);

        Drawable bgDrawable = u.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        u.draw(canvas);
        return returnedBitmap;
    }
}

how to solve this? my output image for before click button,

enter image description here

this is my output 2 for after click the button, enter image description here

  • what u need to do? you want screenshot only? but not shown that in your activity right? – Zaki Pathan Feb 18 '17 at 09:19
  • yes.correct.how to hide showing my screenshot image in activity... –  Feb 18 '17 at 09:20
  • don't use the getBitmapFromview then how to get activity height& width on whole activity? –  Feb 18 '17 at 09:24
  • http://stackoverflow.com/a/32624305/7320259 please try this bro. Hope it helps you – Zaki Pathan Feb 18 '17 at 09:31
  • http://stackoverflow.com/a/5651242/7320259 and try this two. this both are similar. you have to take whole view like this View v1 = getWindow().getDecorView().getRootView(); – Zaki Pathan Feb 18 '17 at 09:32
  • bro above link are "how to take screenshot".but my question are "after taking screenshot that image show in my activity.how to hide it?" this is my question bro.@ Zaki Pathan –  Feb 18 '17 at 09:34
  • If you need screenshot without title bar check this http://stackoverflow.com/a/30212385/7320259 – Zaki Pathan Feb 18 '17 at 09:34
  • you have to try that code. because i test it this works fine in my activity. Please check your further code. this code works fine. I think some other code affect this. please check which code runs after this your functions call – Zaki Pathan Feb 18 '17 at 09:35
  • @zaki pathan.. bro understand my question please.this code working perfectly.i know that.but i want to know "how to hide, screenshot image from activity?????" –  Feb 18 '17 at 09:37
  • you need to hide screenshot which is displaying after clicking button right? you dont need to close your activity right? while using your above code no screenshot is appear on screen. activity is there when i use your code. So some other code affects this and show you a screenshot on your screen. Hope you understand now? – Zaki Pathan Feb 18 '17 at 09:40
  • check this link : https://i.stack.imgur.com/q51wG.png @Zaki pathan this is my final out put image –  Feb 18 '17 at 09:43
  • try using your device not emulator it helps you @Thiru – Zaki Pathan Feb 18 '17 at 09:45
  • no it's not error bro.my need are after taking the screenshot, the image doesn't show in app screen.but it show. –  Feb 18 '17 at 09:58
  • sorry bro my poor english.what i m saying are,after button click the image show in my app.but my need are the image i don't want.then how to hide it? –  Feb 18 '17 at 10:00
  • So no need to set captured screenshot in image. Post your full code please. I dont understand properly what u asking for – Zaki Pathan Feb 18 '17 at 10:01
  • bro.i explain my view of this program.i want to take screenshot for my current activity.i done it.unfortunately after getting screenshot the screenshoted view are here in my activity.but i don't want it.how can i avoid it –  Feb 18 '17 at 10:06
  • you have extra code in your activity? Because I tried your above code in my activity and thats work fine and no view of screenshot is visible in activity. So this code is perfect and working without viewing your captured screenshot in activity. Now you understand what i am telling? – Zaki Pathan Feb 18 '17 at 10:09
  • yes.i understand what you saying.but in my side it will be appear.just a min i update my question with output images.just 2 mins –  Feb 18 '17 at 10:25
  • after clicking button the activity streatch itself.but functionality are work fine.but activity streatched.it's can't be solve??? –  Feb 18 '17 at 10:46
  • you can try by using View v1 = getWindow().getDecorView().getRootView(); full screen view not for your xml layout. Please check my links – Zaki Pathan Feb 18 '17 at 10:47
  • bro before i use that method.but it not work in using scrollview in activity.but my above code work perfectly using scrollview also. –  Feb 18 '17 at 10:50

1 Answers1

0

it's my mistake.after getting screenshot i set layout size on capturing image size(based on get measure width & height)i remove the below line in program it work perfectly.

u.layout(0, 0, u.getMeasuredWidth(), u.getMeasuredHeight());

because that line set saved image exact height & width into current layout.so that's error.i solved it.thanks for your help for all.thanks once again...