Wednesday, July 17, 2024

Debian: using Festival to generate voice from text (Text To Speech/TTS) - AI TTS

folder voice

$ ls /usr/share/festival/voices/english/

Using command interpreter:

$ festival
...
festival> (voice.list)
(kal_diphone)
festival> (SayText "Hello")
#<Utterance 0x7f8747f99ef0>

festival> (quit)

Load text from file output direct to sound

$ festival --tts ./hello.txt 

Load text from file output direct to file

$ text2wave ./hello.txt -o text1.wav

Monday, July 8, 2024

java 17: shuffle 52 playing cards and sort the order base on bridge playing game

This is the code to shuffle 52 playing cards for 4 players and re order each hand cards. I provide to order card base on trump (at right side).

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Test {
    /*
     * TODO how to repesent card
     */
    public final static String[] S_CLUB = {
        "2C",
        "3C",
        "4C",
        "5C",
        "6C",
        "7C",
        "8C",
        "9C",
        "10C",
        "JC", //JC
        "QC", //QC
        "KC", //KC
        "AC"  //AC
    };

    public final static String[] S_DIAMOND = {
        "2D",
        "3D",
        "4D",
        "5D",
        "6D",
        "7D",
        "8D",
        "9D",
        "10D",
        "JD",
        "QD",
        "KD",
        "AD"
    };
    
    public final static String[] S_HEART = {
        "2H",
        "3H",
        "4H",
        "5H",
        "6H",
        "7H",
        "8H",
        "9H",
        "10H",
        "JH",
        "QH",
        "KH",
        "AH"
    };
    
    public final static String[] S_SPADE = {
        "2S",
        "3S",
        "4S",
        "5S",
        "6S",
        "7S",
        "8S",
        "9S",
        "10S",
        "JS",
        "QS",
        "KS",
        "AS"
    };
    
    public static void main(String[] args) {

        long startTime = System.nanoTime();
        shuffleCard();
        long endTime = System.nanoTime();
        System.out.println("North");
        for (int i=0;i<13;i++) {
            System.out.print(north[i]+" ");
        }
        System.out.println();
        System.out.println("After Sorting");
        ArrayList<String> tmp = sortEnd2Begin(north);
        tmp.forEach((String myCard) ->{
            System.out.print(myCard+" ");
        });
        System.out.println();
        

        System.out.println("EAST");
        for (int i=0;i<13;i++) {
            System.out.print(east[i]+" ");
        }
        System.out.println();
        System.out.println("After Sorting");
        tmp = sortEnd2Begin(east);
        tmp.forEach((String myCard) ->{
            System.out.print(myCard+" ");
        });
        System.out.println();
        
        System.out.println("South");
        for (int i=0;i<13;i++) {
            System.out.print(south[i]+" ");
        }
        System.out.println();
        System.out.println("After Sorting");
        tmp = sortEnd2Begin(south);
        tmp.forEach((String myCard) ->{
            System.out.print(myCard+" ");
        });
        System.out.println();
        
        System.out.println("West");
        for (int i=0;i<13;i++) {
            System.out.print(west[i]+" ");
        }
        System.out.println();
        System.out.println("After Sorting");
        tmp = sortEnd2Begin(west);
        tmp.forEach((String myCard) ->{
            System.out.print(myCard+" ");
        });
        System.out.println();

        System.out.println((endTime-startTime)%1000000+" mili"); // time_ms % 1E+3 + " MicroSeconds, "


    }
    
    static String[] north = new String[13];
    static String[] east = new String[13];
    static String[] south = new String[13];
    static String[] west = new String[13];
    
    private static void shuffleCard() {
        //TODO
        ArrayList<String> cards = new ArrayList<>();
        List<String> listCard = Arrays.asList(S_CLUB);
        cards.addAll(listCard);
        listCard = Arrays.asList(S_DIAMOND);
        cards.addAll(listCard);
        listCard = Arrays.asList(S_HEART);
        cards.addAll(listCard);
        listCard = Arrays.asList(S_SPADE);
        cards.addAll(listCard);
        //System.out.println("card number: "+cards.size()); // debug
        
        cards = shuffle(1,cards);
        
        //System.out.println("Distribution "+cards.size());
        int topPosition = cards.size()-1;
        for (int i=0;i<13;i++) {
            //System.out.println("north "+i+" "+topPosition);  // debug
            String tmp = cards.get(topPosition);
            cards.remove(topPosition);
            topPosition --;
            north[i] = tmp;
            //System.out.println("east "+i+" "+topPosition);  // debug
            tmp = cards.get(topPosition);
            cards.remove(topPosition);
            topPosition --;
            east[i] = tmp;
            //System.out.println("south "+i+" "+topPosition);  // debug
            tmp = cards.get(topPosition);
            cards.remove(topPosition);
            topPosition --;
            south[i] = tmp;
            //System.out.println("west "+i+" "+topPosition);  // debug
            tmp = cards.get(topPosition);
            cards.remove(topPosition);
            topPosition --;
            west[i] = tmp;    
        }
        
        
    }
    
    static Random rand = new Random();
    
    private static ArrayList<String> shuffle(int numSuffle, ArrayList<String> cards) {
        //System.out.println("Suffle: "+numSuffle);  // debug
        Collections.shuffle(cards, rand);
        //System.out.println("Card size "+cards.size()); // debug
        
        if (numSuffle>0) {
            shuffle(numSuffle-1, cards);
        }
        
        return cards;
    }
    
    private static ArrayList<String> sortEnd2Begin(String[] myCards) {
        ArrayList<String> myNewCards = new ArrayList<>();
        myNewCards.add(myCards[0]);
        for (int i=1;i<myCards.length;i++) {
            // loop for unordered myCards
            String card2Sort = myCards[i];
            for (int j=0;j<myNewCards.size();j++) {
                // loop for ordered myNewCards
                // find until card2Sort equal or bigger then existing list
                String cardIterate = myNewCards.get(j);
                //int posCard = beforeOrAfter(card2Sort, cardIterate, 0, iterMax);
                int posCard = beforeOrAfter2(card2Sort, cardIterate);
                if (posCard<=0) {
                    myNewCards.add(j, card2Sort);
                    break;
                } else {
                    //System.out.println(j); // debug
                    if (j+1==myNewCards.size()) {
                        // end of list
                        myNewCards.add(card2Sort);
                        break;
                    }
                }
            }

        }
        return myNewCards;
    }
    
    
    /* -1 wordA before wordB
     * 0 wordA equal wordB
     * 1 wordA after wordB
     * suite order less C D H S higher
     * value suit  less 0 1 2 3 4 5 6 7 8 9 A J K Q
     * map 10 to A
     *      J to B
     *      Q to C
     *      K to D
     *      A to E
     */
    private static int beforeOrAfter2(String wordA, String wordB) {
        //System.out.print("word "+wordA+" "+wordB+" "); // debug
        char charA = wordA.charAt(wordA.length()-1);
        char charB = wordB.charAt(wordB.length()-1);
        //System.out.println(wordA+" "+wordB+" End "+charA+" "+charB+" "); // debug
        if (charA<charB) {
            return -1;
        } else if (charA>charB) {
            return 1;
        } else {
            // equal
            charA = wordA.charAt(wordA.length()-2);
            charB = wordB.charAt(wordB.length()-2);
            // mapping charA to correct order
            if (charA=='0') {
                charA='A';
            } else if (charA=='J') {
                charA='B';
            } else if (charA=='Q') {
                charA='C';
            } else if (charA=='K') {
                charA='D';
            } else if (charA=='A') {
                charA='E';
            }
            // mapping charB to correct order
            if (charB=='0') {
                charB='A';
            } else if (charB=='J') {
                charB='B';
            } else if (charB=='Q') {
                charB='C';
            } else if (charB=='K') {
                charB='D';
            } else if (charB=='A') {
                charB='E';
            }
            
            //System.out.println(" First "+charA+" "+charB); // debug
            if (charA<charB) {
                return -1;
            } else if (charA>charB) {
                return 1;
            }
        }
        
        return 0;
    }

    public static ArrayList<String> sortCardsWithTrump(ArrayList<String> myCards, char trump) {
        ArrayList<String> myNewCards = new ArrayList<>();
        myNewCards.add(myCards.get(0));
        for (int i=1;i<myCards.size();i++) {
            // loop for unordered myCards
            String card2Sort = myCards.get(i);
            for (int j=0;j<myNewCards.size();j++) {
                // loop for ordered myNewCards
                // find until card2Sort equal or bigger then existing list
                String cardIterate = myNewCards.get(j);
                //int posCard = beforeOrAfter(card2Sort, cardIterate, 0, iterMax);
                int posCard = beforeOrAfter2Trump(card2Sort, cardIterate, trump);
                if (posCard<=0) {
                    myNewCards.add(j, card2Sort);
                    break;
                } else {
                    //System.out.println(j); // debug
                    if (j+1==myNewCards.size()) {
                        // end of list
                        myNewCards.add(card2Sort);
                        break;
                    }
                }
            }

        }
        return myNewCards;
    }
    
    /* -1 wordA before wordB
     * 0 wordA equal wordB
     * 1 wordA after wordB
     * NO TRUMP CDHS
     * TRUMP Map to Z for trump
     * suite order less C D H S higher
     * value suit  less 0 1 2 3 4 5 6 7 8 9 A J K Q
     * map 10 to A
     *      J to B
     *      Q to C
     *      K to D
     *      A to E
     */
    private static int beforeOrAfter2Trump(String wordA, String wordB, char trump) {
        //System.out.print("word "+wordA+" "+wordB+" "); // debug
        char charA = wordA.charAt(wordA.length()-1);
        char charB = wordB.charAt(wordB.length()-1);
        //System.out.println(wordA+" "+wordB+" End "+charA+" "+charB+" "); // debug
        // remap char for TRUMP
        // mapping charA to correct order
        if (charA==trump) {
            charA='Z';
        }
        // mapping charB to correct order
        if (charB==trump) {
            charB='Z';
        }
        if (charA<charB) {
            return -1;
        } else if (charA>charB) {
            return 1;
        } else {
            // equal
            charA = wordA.charAt(wordA.length()-2);
            charB = wordB.charAt(wordB.length()-2);
            // mapping charA to correct order
            if (charA=='0') {
                charA='A';
            } else if (charA=='J') {
                charA='B';
            } else if (charA=='Q') {
                charA='C';
            } else if (charA=='K') {
                charA='D';
            } else if (charA=='A') {
                charA='E';
            }
            // mapping charB to correct order
            if (charB=='0') {
                charB='A';
            } else if (charB=='J') {
                charB='B';
            } else if (charB=='Q') {
                charB='C';
            } else if (charB=='K') {
                charB='D';
            } else if (charB=='A') {
                charB='E';
            }
           
            //System.out.println(" First "+charA+" "+charB); // debug
            if (charA<charB) {
                return -1;
            } else if (charA>charB) {
                return 1;
            }
        }
        return 0;
    }

}


 

Friday, July 5, 2024

java 17: sort 2 strings from end to begin of word

This code will sort string array and return a new arraylist with order word (ascending), but ordered done from end of each word. This code work like sorting file name in file manager.

Source code:

public class WordC {
    // sort from end to begin

    public static void main(String[] args) {
                String[] sTest = {"4H", "5H", "8C", "8D", "9H", "9D", "AC", "6S", "5D", "6D", "KD", "10C", "AD" };
       
        for (int i=0;i<13;i++) {
            System.out.print(sTest[i]+" ");
        }
        System.out.println();
        System.out.println("Sorting");
        ArrayList<String> tmp = sortEnd2Begin(sTest);
        tmp.forEach((String myCard) ->{
            System.out.print(myCard+" ");
        });
        System.out.println();
    }
       
    private static ArrayList<String> sortEnd2Begin(String[] myCards) {
        ArrayList<String> myNewCards = new ArrayList<>();
        myNewCards.add(myCards[0]);
        for (int i=1;i<myCards.length;i++) {
            // loop for unordered myCards
            String card2Sort = myCards[i];
            for (int j=0;j<myNewCards.size();j++) {
                // loop for ordered myNewCards
                // find until card2Sort equal or bigger then existing list
                String cardIterate = myNewCards.get(j);
                int iterMax = card2Sort.length();
                if (card2Sort.length() > cardIterate.length()) {
                    iterMax = cardIterate.length();
                }
                int posCard = beforeOrAfter(card2Sort, cardIterate, 0, iterMax);
                if (posCard==0 || posCard<0) {
                    myNewCards.add(j, card2Sort);
                    break;
                } else {
                    // TODO
                    //System.out.println(j); // debug
                    if (j+1==myNewCards.size()) {
                        // end of list
                        myNewCards.add(card2Sort);
                        break;
                    }
                }
            }

        }
        return myNewCards;
    }
       
    // -1 wordA before wordB
    // 0 wordA equal wordB
    // 1 wordA after wordB
    private static int beforeOrAfter(String wordA, String wordB, int iterX, int iterMax) {
        //System.out.println(wordA+" "+wordB+" "+iterX+" "+iterMax);
        int inA = wordA.length()-1-iterX;
        int inB = wordB.length()-1-iterX;
        char cA = wordA.charAt(inA);
        char cB = wordB.charAt(inB);
        int retValue = 0;
        if (iterX+1==iterMax) {
            // we reach max recursive {
            //System.out.println(iterX+" "+iterMax+" "+cA+" "+cB); // debug
            if (cA==cB) {
                //System.out.println("Debug"); // debug
                if (wordA.length()<wordB.length())  {
                    retValue = -1;
                } else if (wordA.length()>wordB.length())  {
                    retValue = 1;
                } else {
                    retValue = 0; // equal letters and length
                }
            } else if (cA<cB) {
                //retValue = -1;  // ori
            } else if (cA>cB) {
                retValue = 1; // ori
            }
        } else {
            if (cA<cB) {
                //System.out.println(cA+" < "+cB+" "+(cA < cB)); // debug
                retValue = -1; // ori
            } else if (cA>cB) {
                //System.out.println(cA+" > "+cB+" "+(cA > cB)); // debug
                retValue = 1; // ori

            } else {
                //System.out.println(cA+" = "+cB+" "+(cA == cB)); // debug
                retValue = beforeOrAfter(wordA, wordB, iterX+1, iterMax);
            }
        }
        return retValue;
    }

}


Tuesday, June 25, 2024

java 17: tutorial network socket

NOTE for socket:

  1. Socket can not be use to read and send at the same time
  2. Only one state can be use in socket, client state send - server state read or vise versa.
  3. At server side, maintain state read and send for each connection accepted by serversocket.

Swing JFrame to start server and client

package com.dedetok;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


import javax.swing.BoxLayout;
import javax.swing.JButton;

public class Test extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test frame = new Test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    MyServerThread myServerThread;
    ExecutorService executor;
    MyClient myClient=null;

    /**
     * Create the frame.
     */
    public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
       
        JButton btnStartServer = new JButton("Start Server");
        contentPane.add(btnStartServer);
        btnStartServer.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                //System.out.println("Test"); // debug
                // TODO Auto-generated method stub
                if (executor==null) {
                    executor = Executors.newSingleThreadExecutor();
                    try {
                        if (myServerThread==null) {
                            System.out.println("Server null, create new"); // debug
                            myServerThread = new MyServerThread();
                            executor.submit(myServerThread);
                        }
                        //System.out.println("create thread"); // debug
                        //System.out.println("run thread"); // debug
                        if (executor.isTerminated()) {
                            System.out.println("is terminated"); // debug
                           
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                   
                }
            }
           
        });
       
        JButton btnStartClient = new JButton("Start Client");
        contentPane.add(btnStartClient);
        btnStartClient.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try {
                    /*
                    if (myClient== null) {
                        myClient = new MyClient();
                    }
                    myClient.sendCommand();
                    */

                    myClient = new MyClient();
                    myClient.sendCommand();
                   
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
           
        });
    }

}

Server part

package com.dedetok;


import java.io.IOException;
import java.io.InputStream;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServerThread implements Runnable {

    ServerSocket myServerSocket;
    //InetAddress myServerInetAddress;
    int myServerPort = 54321;
    
    public MyServerThread() throws IOException {
        myServerSocket = new ServerSocket(myServerPort);
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (true) {

            Socket myClientSocket;
            try {
               
                myClientSocket = myServerSocket.accept();
               
                /*
                InetAddress inetAddress = myClientSocket.getInetAddress();
                System.out.println("receive from client: "+inetAddress.toString()); // debug
               
                InputStream myInputStream = myClientSocket.getInputStream();
                ObjectInputStream ois = new ObjectInputStream(myInputStream);
               
                String sClient;

                sClient = (String) ois.readObject();

                System.out.println("From Client: "+sClient); // debug
                sClient = "Echo: "+sClient;

                OutputStream myOutputStream = myClientSocket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(myOutputStream);
                oos.writeObject(sClient);
                oos.flush();


                //ois.close();
                //oos.close();
                myClientSocket.close();
                */
               
                ClientHandler clientHandler = new ClientHandler(myClientSocket);
                clientHandler.run();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    class ClientHandler implements Runnable {
        Socket socket;
        ClientHandler(Socket socket) {
            this.socket = socket;
        }
       
        @Override
        public void run() {
            // TODO Auto-generated method stub
            InetAddress inetAddress = socket.getInetAddress();
            System.out.println("receive from client: "+inetAddress.toString()); // debug
           
            InputStream myInputStream;
            try {
                myInputStream = socket.getInputStream();
                ObjectInputStream ois = new ObjectInputStream(myInputStream);
                OutputStream myOutputStream = socket.getOutputStream();

                ObjectOutputStream oos = new ObjectOutputStream(myOutputStream);
           
                boolean loopMsg = true;

                while (loopMsg) {
                    String sClient = (String) ois.readObject();
                    if (sClient.equals("END")) {
                        loopMsg = false;
                    }
                    System.out.println("From Client: "+sClient); // debug
                    sClient = "Echo: "+sClient;
    
                    oos.writeObject(sClient);
                    oos.flush();
                   
                }


            //ois.close();
            //oos.close();
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
       
    }

}

Client part

package com.dedetok;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class MyClient {

    ExecutorService executor;
    boolean isRun = false;
    
    public MyClient() throws IOException {
        executor  = Executors.newSingleThreadExecutor();

    }
    
    public void sendCommand() {
        if (!isRun) {
            executor.submit(myRun);
            isRun = true;
        }

    }
    
    int i=0;
    String str = "Hello ";
    
    Runnable myRun = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            //System.out.println("Running thread client"); // debug
            try {

                /*
                System.out.println("create socket"); // debug
                Socket mySocket = new Socket("127.0.0.1", 54321);
               
                OutputStream myOutputStream = mySocket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(myOutputStream);

               
                    System.out.println("send "+i); // debug

                        oos.writeObject("END");
                    oos.writeObject(str+1);
                    oos.flush();
                   
                    InputStream myInputStream = mySocket.getInputStream();
                    ObjectInputStream ois = new ObjectInputStream(myInputStream);
                    System.out.println("receive "+i); // debug
                    String rStr = (String) ois.readObject();
                    System.out.println(rStr); // debug
                   
                   
                System.out.println("end client"); // debug
                //mySocket.close();
               
                //ois.close();
                //oos.close();
                mySocket.close();
                i++;
                */
               
                System.out.println("create socket"); // debug
                Socket mySocket = new Socket("127.0.0.1", 54321);
               
                OutputStream myOutputStream = mySocket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(myOutputStream);

                InputStream myInputStream = mySocket.getInputStream();
                ObjectInputStream ois = new ObjectInputStream(myInputStream);
               
                while (i<10) {
                    String str = "send "+i;
                    System.out.println(str); // debug

                    if (i==9)
                        str="END";
                    oos.writeObject(str);                       
                    oos.flush();
                   

                    System.out.println("receive "+i); // debug
                    String rStr = (String) ois.readObject();
                    System.out.println(rStr); // debug
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    //mySocket.close();
                   
                    //ois.close();
                    //oos.close();
                   
                    i++;
                }
                mySocket.close();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            isRun = false;
        }
       
    };
}



Monday, June 3, 2024

Android java: request multiple permission READ_EXTERNAL_STORAGE & WRITE_EXTERNAL_STORAGE

Minimum API 24 (Android 7.0) 

Note:

  • Beginning with Android 4.4 (API level 19) it's no longer necessary for your app to request the WRITE_EXTERNAL_STORAGE permission to write to its own application-specific directories on external storage, which are provided by getExternalFilesDir(). However, the permission is required for API level 18 and lower. Example:
    File extDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
  • If the permission is a runtime permission or special permission, and if your app is installed on a device that runs Android 6.0 (API level 23) or higher, you must request the runtime permission or special permission yourself.
  • On devices that run Android 4.4 (API level 19) and higher, your app can interact with a documents provider, including external storage volumes and cloud-based storage, using the Storage Access Framework. This framework allows users to interact with a system picker to choose a documents provider and select specific documents and other files for your app to create, open, or modify.
  • To support media file access on devices that run Android 9 (API level 28) or lower, declare the READ_EXTERNAL_STORAGE permission and set the maxSdkVersion to 28. See reference 1.
  • If your app targets Android 10 (API level 29) or lower, you can temporarily opt out of scoped storage in your production app. If you target Android 10, however, you need to set the value of requestLegacyExternalStorage to true in your app's manifest file.

AndroidManifest.xml

...
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:maxSdkVersion="28"
        />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28"
        />
...
    <!-- This attribute is "false" by default on apps targeting Android 10. -->
    <!-- android:requestLegacyExternalStorage="true" -->
    <application
        android:requestLegacyExternalStorage="true"
...

MainActivity.java

...
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        // android:maxSdkVersion="28" =  Build.VERSION_CODES.P
        if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
            checkMyPermission();
        }
...    int MY_CODE_REQUEST = 123;
    /*
     * 1. check permission
     */
    private void checkMyPermission() {
        // Build.VERSION.SDK_INT >= 23
        // https://riptutorial.com/android/example/23932/multiple-runtime-permissions-from-same-permission-groups
        ArrayList<String> sPermissionRequest = new ArrayList<>();
        int myRead = ContextCompat.checkSelfPermission(getApplicationContext()
                , READ_EXTERNAL_STORAGE);
        int myWrite = ContextCompat.checkSelfPermission(getApplicationContext()
                , WRITE_EXTERNAL_STORAGE);
        if (myRead != PackageManager.PERMISSION_GRANTED) {
            sPermissionRequest.add(getString(R.string.s_permission_read_storage));
        }
        if (myWrite != PackageManager.PERMISSION_GRANTED) {
            sPermissionRequest.add(getString(R.string.s_permission_write_storage));
        }

        if (sPermissionRequest.size()>0) {
            // NOT Granted
            String[] sPermissions = new String[sPermissionRequest.size()];
            for (int i=0;i<sPermissionRequest.size();i++) {
                sPermissions[i]=sPermissionRequest.get(i);
            }
            //sLog.e("dedetok", "sPermissionRequest.size()>0 size "+sPermissions.length); // debug
            requestPermissions(sPermissions, MY_CODE_REQUEST);
        }
    }

    /*
     * 2. receive permission status
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode==MY_CODE_REQUEST) {

            if (permissions.length>0) {
                ArrayList<String> myPermission = new ArrayList<>();
                for (int i=0;i<permissions.length;i++) {
                    //Log.e("dedetok", permissions[i]+" "+grantResults[i]); // debug
                    if (permissions[i].equals(getString(R.string.s_permission_read_storage)) &&
                            grantResults[i]!=PackageManager.PERMISSION_GRANTED) {
                        myPermission.add(READ_EXTERNAL_STORAGE);
                    }
                    if (permissions[i].equals(getString(R.string.s_permission_write_storage)) &&
                            grantResults[i]!=PackageManager.PERMISSION_GRANTED) {
                        myPermission.add(WRITE_EXTERNAL_STORAGE);
                    }
                }
                // Not granted, Request permissions on runtime

                String[] sRequestPermission = new String[myPermission.size()];
                for (int i=0;i<myPermission.size();i++) {
                    sRequestPermission[i] = myPermission.get(i);
                }

                //Log.e("dedetok", "requestCode==MY_CODE_REQUEST "+sRequestPermission.length); // debug
                requestPermissionLauncher.launch(sRequestPermission);
            }
        }
    }

    /*
     * 3. callback user permission
     * Register the permissions callback, which handles the user's response to the
     * system permissions dialog. Save the return value, an instance of
     * ActivityResultLauncher, as an instance variable.
     */
    private final ActivityResultLauncher<String[] > requestPermissionLauncher =
            registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions()
                    , new ActivityResultCallback() {
                        @Override
                        public void onActivityResult(Object objMap) {
                            //Log.e("dedetok", "onActivityResult "+o.toString()); // debug
                            if (objMap instanceof Map) {
                                StringBuilder mySB = new StringBuilder();
                                Map<String,Boolean> myMapPermission = (Map<String,Boolean>) objMap;
                                myMapPermission.forEach((permissionKey, permissionBoolean)->{
                                    //Log.e("dedetok", permissionKey+" "+READ_EXTERNAL_STORAGE+" "+permissionBoolean); // debug
                                    if (permissionKey.equals(READ_EXTERNAL_STORAGE) && !permissionBoolean) {
                                        mySB.append(READ_EXTERNAL_STORAGE+" ");
                                    }
                                    if (permissionKey.equals(WRITE_EXTERNAL_STORAGE) && !permissionBoolean) {
                                        mySB.append(WRITE_EXTERNAL_STORAGE+" ");
                                    }
                                });
                                //Log.e("dedetok", "mySB "+mySB.toString()); // debug
                                if (mySB.length()>0) {
                                    updateStatus(mySB.toString());
                                }
                            }
                        }
                    });

 References:

  1. https://developer.android.com/training/data-storage/shared/documents-files
  2. https://developer.android.com/training/permissions/declaring
  3. https://developer.android.com/guide/topics/manifest/uses-permission-element
  4. https://stackoverflow.com/questions/3093365/how-can-i-check-the-system-version-of-android
  5. https://developer.android.com/training/data-storage/use-cases