0

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
Dominik
  • 55
  • 1
  • 2
  • 7
  • You need to constuct an instance of `DateFormat`. It doesn't have a static `format()` method. Not sure what they're using in the video. Maybe a custom class? – shmosel Aug 30 '23 at 21:58
  • OI strongly recommend that you don’t use `DateFormat`. It’s a long outdated class that was always notoriously troublesome. Depending on your needs look into `LocalDateTime`, `DateTimeFormatter` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Aug 30 '23 at 21:59
  • Welcome to Stack Overflow. What was the purpose of that line, `CharSequence format = DateFormat.format("yyyy-MM-dd_HH:mm:ss");`? Surely the `java.text.DateFormat` never was able to take a screenshot?! If we’re to help, we need to know what you were trying to do. And I am not looking through that video (way too many ads in the way). – Ole V.V. Aug 30 '23 at 22:06
  • 1
    The video is for Android development. The Android SDK has a class `android.text.format.DateFormat` which has a method [`public static CharSequence format(CharSequence, Date)`](https://www.android-doc.com/reference/android/text/format/DateFormat.html#format(java.lang.CharSequence,%20java.util.Date)) which means that your import of `java.text.DateFormat` is wrong. – Thomas Kläger Aug 31 '23 at 05:35

0 Answers0