Search code examples
androidscreenshot

android activity screenshot how?


Hello Friends i'm making a app with webview i want to take screenshot of my activity Currnetly i'm using this code for capture image

public Bitmap takeScreenshot() {
   View rootView = findViewById(android.R.id.content).getRootView();
   rootView.setDrawingCacheEnabled(true);
   return rootView.getDrawingCache();
}

And This for Save

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

It sometime Works and sometime not, i mean ,sometime i can see in gallery screenshot and sometime not i want to if there is any option to show captured screenshot Like whhen we press Vol+power button

Main problem is how can i show image when its taken or know if its taken or not Thanks in advance


Solution

  • This is a sample approach: save your image -> display result with dialog. And to make it available in Android Gallery, the file path should also be changed:

    public void saveBitmap(Bitmap bitmap) {
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File imagePath = new File(path, "screenshot.png");//now gallery can see it
    FileOutputStream fos;
     try {
         fos = new FileOutputStream(imagePath);
         bitmap.compress(CompressFormat.JPEG, 100, fos);
         fos.flush();
         fos.close();
    
         displayResult(imagePath.getAbsolutePath())// here you display your result after saving
     } catch (FileNotFoundException e) {
         Log.e("GREC", e.getMessage(), e);
     } catch (IOException e) {
         Log.e("GREC", e.getMessage(), e);
     }
    }
    

    Create another method call displayResult to see the result:

    public void displayResult(String imagePath){
    
        //LinearLayOut Setup
        LinearLayout linearLayout= new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
    
        linearLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
    
        //ImageView Setup
        ImageView imageView = new ImageView(this);//you need control the context of Imageview
    
        //Diaglog setup
        final Dialog dialog = new Dialog(this);//you need control the context of this dialog
        dialog.setContentView(imageView);
    
       imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
    
       //Display result
       imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
       dialog.show();
    
    }