-1

I'm developing an application that displays .dxf files (using Kabeja library). I've done all that part and everything works just as I wanted to. Problem is, I need to have a button that opens a file browser so the user can import his own .dxf from the sd card or local storage. It needs to filter files to only display .dxf so no other extension can be imported. I have absolutely no idea how to do this, nor to make a basic file browser. Could you help me getting on the right track ?

Thanks

Gogolplex
  • 11
  • 5
  • 1
    Did you try Intent and specify which extension you want to work on ? – Khalid Ali May 02 '18 at 07:07
  • 1
    Related: https://stackoverflow.com/questions/9923760/how-to-use-intent-for-choosing-file-browser-to-select-file – sirius May 02 '18 at 07:10
  • Well I didn't as I don't really know how to access my storage and to get the files in the first place. I've seen different options such as using already existing file choosers or handling the file choice with a different activity but I don't know how to apply them to my needs. I'm new to android so not familiar with all the specific classes and mechanisms – Gogolplex May 02 '18 at 07:13
  • On the internet and github you can find code for file pickers/browsers which you can include in your app. – greenapps May 02 '18 at 07:29

2 Answers2

0

Try this

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");

// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getPackageManager()) != null) {
   startActivityForResult(intent, 1);
 }

And if you want to enforce an app chooser

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");

Intent chooser = Intent.createChooser(sendIntent, "Title");


// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getPackageManager()) != null) {
   startActivityForResult(chooser, 1);
 }
Khalid Ali
  • 373
  • 1
  • 15
  • Okay thanks a lot, just about the `intent.setType(String type)` method, do I have to pass a MIME type ? because `"file/*"` makes every file disabled. I tried `"*/*"` and every file is enabled, but as I want to have only .dxf enable I thought it would be `"*/dxf"` but it doesn't seem to work. – Gogolplex May 02 '18 at 07:32
  • try this application/dxf – Khalid Ali May 02 '18 at 08:10
  • also try image/vnd.dwg image/x-dwg – Khalid Ali May 02 '18 at 08:11
  • doesn't work. it's gotta be "application/something" as "application/*" works but isn't restrictive enough – Gogolplex May 02 '18 at 08:27
  • so you should write one more condition and check if the file extension is not what you want show a message to user you select wrong file at least for this time – Khalid Ali May 02 '18 at 08:33
0
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*"); // all images
//intent.setType("image/png"); // only pngs
startActivityForResult(intent);

And get response in onActivityResult method. More details in documentation.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36