Serj Sagan
Serj Sagan

Reputation: 30208

Prevent file name changes in chooser of Intent.ACTION_CREATE_DOCUMENT

I am using the following code to have a user select where to export a file:

private void onChooseFile() {
  SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");
  Intent intent = new Intent()
    .setType("*/*")
    .setAction(Intent.ACTION_CREATE_DOCUMENT)
    .putExtra(Intent.EXTRA_TITLE, dateFormatter.format(new Date()) + "-Whatever.mydb");

  Intent chooser = Intent.createChooser(intent, "Select a file");

  startActivityForResult(chooser, FILE_SELECT_CODE);
}

This works unless a user decides to delete my default provided file name... then it creates a file called literally "(invalid)" at the selected location... how do I prevent users changing my default provided name... my real intent is to make sure that the file that I export is never called "(invalid)" and always ends with the "mydb" extension.

Upvotes: 0

Views: 188

Answers (2)

blackapps
blackapps

Reputation: 9282

I am using the following code to have a user select where to export a file:

Not only that but you give them also the power to determine the file name.

Instead you should use ACTION_OPEN_DOCUMENT_TREE to .. to have a user select where to export a file.

The user then selects the directory upon which you can write a file in it with a suitable name.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007099

None of what you want is possible. It is the user's device and the user's content. The user can name it whatever the user wants.

Upvotes: 0

Related Questions