0

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rewind
  • 2,554
  • 3
  • 30
  • 56

2 Answers2

0

Try this :

    Intent intent = new Intent();
    intent.setType("application/pdf");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
0

For custom type you can use application/mfl so your Intent should be:

//Use ACTION_OPEN_DOCUMENT to get read/write permission
Intent intent = new Intent(Intent.Intent.ACTION_OPEN_DOCUMENT);
//"application/mfl" to request only .mfl files
intent.setType("application/mfl");
//Request readable files
intent.addCategory(Intent.CATEGORY_OPENABLE)
startActivityForResult(Intent.createChooser(intent, "Select File"),PICK_MYFILE_REQUEST);
Jordy Mendoza
  • 432
  • 4
  • 16