@CommonsWare
Update #4: Finally got to run ContentEditor. Main holdup was getting correct Java version specified. Update #3: Reading your book from the start. Perhaps now I will get somewhere. I'll come back to this later. Update #2: BTW, I have downloaded the code for the ContentEditor, unzipped it, opened it with Android Studio. It compiled with nice green checkmarks on each part :) but I have not figured out how to actually run it. I suspect I have to create a run configuration for it? Kind of stumped on the next step here :(
Update #1: One brief look at the chapter you linked and I saw one, obvious now, error in my code. ACTION_CREATE_DOCUMENT where I should have had ACTION_OPEN_DOCUMENT in the save_stack() intent. I fixed that.
Additionally, what I overlooked is these bits of code in onPause and onResume... onPause:
File stackRoot = new File(extFilesDir, "stack.txt");
if (!stackRoot.delete())
toast("Mickx: No stack.txt to delete");
if (!stack.isEmpty())
save_Stack_intent();
onResume:
File stackRoot = new File(extFilesDir, "stack.txt");
if (stackRoot.exists()) {
restore_Stack_intent();
}
This is the only place from which these methods are called, so I will move this code into the beginning of the corresponding methods in onActivityResult. My question is how to I determine if stack.txt exists? and how to delete it? I found this.. How to save and check if the file exists in scoped storage? and Migration from File Api to Storage Access Framework (SAF) in Android? but did not find them helpful :(
These files i need to determine if exist and/or delete...
OutputStream outputStream = getContentResolver().openOutputStream(data.getData());
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
and
inputStream inputStream = getContentResolver().openInputStream(data.getData());
BufferedInputStream bis = new BufferedInputStream(inputStream);
BufferedReader br = new BufferedReader(new InputStreamReader(bis));
Here is the new code for save_stack(), restore_stack() and onActivityResult...
public void save_Stack_intent() { // to stack.txt
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, "stack.txt");
startActivityForResult(intent, CODE_SAVE_STACK);
}
public void restore_Stack_intent() { // from stack.txt
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, "stack.txt");
startActivityForResult(intent, CODE_RESTORE_STACK);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
if (requestCode == CODE_SAVE_STACK) { // from memory to stack.txt
try {
OutputStream outputStream = getContentResolver().openOutputStream(data.getData());
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
String lineSeparator = System.getProperty("line.separator");
for (String item : stack) {
byte[] byteArray = item.getBytes();
bos.write(byteArray);
assert lineSeparator != null;
bos.write(lineSeparator.getBytes());
}
toast_Short("The Stack is saved");
bos.close();
} catch (IOException e) {
toast("IOException: " + e);
toast("The Stack is not saved!");
}
} else if (requestCode == CODE_RESTORE_STACK) { // from stack.txt to memory
try {
inputStream inputStream = getContentResolver().openInputStream(data.getData());
BufferedInputStream bis = new BufferedInputStream(inputStream);
BufferedReader br = new BufferedReader(new InputStreamReader(bis));
String str;
while ((str = br.readLine()) != null) {
stack.add(str);
}
br.close();
toast_Short("The stack is restored");
} catch (IOException e) {
toast("IOException: " + e);
}
}
}
}
Following the chapter from your book that you linked I came up with this code at the beginning of save_stack() as a start, in case I have to delete...
DocumentFile docFile = DocumentFile.fromSingleUri(data.getData());
assert docFile != null;
if (docFile.canWrite()) docFile.delete();
It says that fromSingleUri() requires a parameter of type Context :( Q: How do I get that parameter? I expect that I really do not understand what is in data.getData. I thought it was a URI. Hope you can help, Mick