I can find some very old file-picker samples, but think it might now be possible to do it with the more up to date Intent
.
I want to load and save files. It will be a text file but with an extension .mfl
(an extension I am using for my files).
I have seen someone's code for images (but just for loading):
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
What do I need in setType
to only see my .mfl files?
I already have a onActivityResult
doing other stuff, so need to create my own requestCode. I was thinking
int PICK_MYFILE_REQUEST = 1001;
So I think my onActivityResult
needs to look like this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(/* doing my other stuff */){
} else if (requestCode == PICK_MYFILE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
/* load the file */
} catch (IOException e) {
e.printStackTrace();
}
}
}
As you can see I think I might be almost there, but not completely sure what I am meant to be doing. (I am new to Android - I think you can guess.)
Is this roughly right? What do I need to do to get this working?
Also, how do I modify this to get a file saver dialog?