I tried to make screenshot programatically according to this video but there is error with CharSequence format:

error: non-static method format(Object) cannot be referenced from a static context
        CharSequence format = DateFormat.format("yyyy-MM-dd_HH:mm:ss");

I tried to find it in documentation but I was not succesfull. I tried chat gpt but i received same code as I sent him...

package com.example.screenshot_new;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    Button btn;

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

        verifyStoragePermission(this);
        btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                takeScreenShot(getWindow().getDecorView().getRootView(), "result");
            }

        });
    }

    protected static File takeScreenShot(View view, String fileName) {
        Date date = new Date();
        CharSequence format = DateFormat.format("yyyy-MM-dd_HH:mm:ss");
        try {
            String dirPath = Environment.getExternalStorageDirectory().toString() + "/learnwithDeeksha";
            File fileDir = new File(dirPath);
            if(!fileDir.exists()){
                boolean mkdir=fileDir.mkdir();
          //other code
0

There are 0 best solutions below