'Im trying to use createFile to make .txt file without system picker just the dialog from a path is this possible?

I dont want to use picker since this is a notepad app. And this will be after user fills in folder fields onstop activity it writes to folder they choose. It has to not be public since they can choose and has to be there choice without picker. Ive seen the folder ok dialog, i would like to use that.

Code

Intent intent = new 
Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/txt");
intent.putExtra(Intent.EXTRA_TITLE, "invoice.txt");
ActivityResultContracts.CreateDocument();


Solution 1:[1]

I figured it out, in case someone has problems.

try{
Uri treeUri = Uri.parse(muri);
final int takeFlags = intent.getFlags() & 
(Intent.FLAG_GRANT_READ_URI_PERMISSION | 
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
this.getContentResolver().
takePersistableUriPermission(treeUri, takeFlags);
DocumentFile docFile = DocumentFile.fromTreeUri(this, 
treeUri);
filename = "JAMZ.txt"; 
DocumentFile resultFile = 
docFile.createFile("application/txt", filename);
If (resultFile == null) { resultFile = 
docFile.findFile(filename + ".txt"); }
OutputStream os = this.getContentResolver().
openOutputStream(resultFile.getUri()); 
os.write(pass.getBytes());
os.flush();
os.close(); 
} catch (Exception e) {}

Solution 2:[2]

One of the way is to use below logic

        while (startInstant.truncatedTo(ChronoUnit.DAYS).isBefore(endInstant.truncatedTo(ChronoUnit.DAYS))) {

        Instant startDayInstant = startInstant.truncatedTo(ChronoUnit.DAYS);
        Instant nextDayInstant = startDayInstant.plus(1, ChronoUnit.DAYS);
        timeRanges.add(new TimeRange(startDayInstant, startInstant, nextDayInstant.minusMillis(1)));
        startInstant = nextDayInstant;

    }
    timeRanges.add(new TimeRange(startInstant.truncatedTo(ChronoUnit.DAYS),
                    startInstant, endInstant));
    System.out.println(timeRanges);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J. M.
Solution 2 JAMBUGOLA MAHESH