Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take screenshot of a layout in android

I want to save my frame layout into gallery by taking the screen shot of the layout. This code works on a project(Name: Trial), but when I tried the same code on othe project(Name: Fg), It did not work. The photo is not being saved in the Gallery.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_background);




}
public Bitmap click(View view) {
    FrameLayout memecontentView =(FrameLayout) findViewById(R.id.my);
    View view1 = memecontentView;

    Bitmap b = Bitmap.createBitmap(view1.getWidth(), view1.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    view1.draw(canvas);


    String extr = Environment.getExternalStorageDirectory().toString();
    File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
    FileOutputStream fos = null;

    try {
        fos = new FileOutputStream(myPath);
        b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), b,
                "Screen", "screen");

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return b;


}

}

like image 529
Adistark Avatar asked Dec 23 '15 22:12

Adistark


People also ask

How do you take a screenshot of a specific screen on Android?

Take a scrolling screenshot Open the screen that you want to capture. Press the Power and Volume down buttons at the same time. At the bottom, tap Capture more. To select the content you want to capture, use the crop guidelines.

How do I take a screenshot of a view and save it to gallery Android?

Take screenshots with Android 12 Hold down the power button and press the volume-down button.

How do you screenshot on Android 11 home screen?

Press the Power and Volume down buttons at the same time. Your phone will take a picture of the screen and save it. At the bottom left, you'll find a preview of your screenshot.


1 Answers

Have you got the

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Declared in your manifest?

Also if you want the entire view try this:

view1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view1.getDrawingCache());

Oh and this may save you some time:

private void openScreenshot(File imageFile) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(imageFile);
    intent.setDataAndType(uri, "image/*");
    startActivity(intent);
}

Src: How to programmatically take a screenshot in Android?

like image 137
KoalaKoalified Avatar answered Oct 08 '22 13:10

KoalaKoalified