user3565993
user3565993

Reputation: 35

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

Upvotes: 2

Views: 1438

Answers (2)

T D Nguyen
T D Nguyen

Reputation: 7603

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();

}

Upvotes: 2

Dave S
Dave S

Reputation: 3468

Other people have run into issues when trying to get a bitmap from a view's Drawing Cache.

Rather than trying to use the drawing cache, you can instead just get the bitmap directly from the view. Google's DynamicListView example uses the following function to do so.

 /** Returns a bitmap showing a screenshot of the view passed in. */
private Bitmap getBitmapFromView(View v) {
    Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas (bitmap);
    v.draw(canvas);
    return bitmap;
}

Upvotes: 0

Related Questions