Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACTION_GET_CONTENT only files, no images or videos

Tags:

java

file

android

Ok i start my func with this

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType(mimeType);
    intent.addCategory(Intent.CATEGORY_OPENABLE);

for Samsung

        chooserIntent = Intent.createChooser(sIntent, context.getString(R.string.choosefilefrom));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {intent});

for other devices

        chooserIntent = Intent.createChooser(intent, context.getString(R.string.choosefilefrom));

mimeType are the types are allowed to use, its at this moment */*. but how to exclude the images and videos to show only files in the file picker? i have 3 intents, one for images, one for videos and one for files.

like image 967
CoXx Industries Avatar asked Oct 18 '25 11:10

CoXx Industries


2 Answers

If you take a look at https://www.iana.org/assignments/media-types/media-types.xhtml

Then you can probably assume the main mime types:

  • application/* (A lot of file types, excluding below main types, But includes audio like application/ogg)
  • audio/*
  • font/*
  • image/*
  • message/*
  • model/*
  • multipart/*
  • text/*
  • video/*

So assuming you want to be able to select anything else other then Audio or Video or perhaps Images

Then the following should do the trick:

String[] mimetypes = {
    "application/*",
    //"audio/*",
    "font/*",
    //"image/*",
    "message/*",
    "model/*",
    "multipart/*",
    "text/*",
    //"video/*"
};

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes); //Important part here
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, 10000);

Then of course to handle the returned URIs:

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 10000)
    {
        if (data != null)
        {
            if (data.getClipData() != null)
            {
                // multiple files selected
                for (int i = 0; i < data.getClipData().getItemCount(); i++)
                {
                    android.net.Uri uri = data.getClipData().getItemAt(i).getUri();
                }
            }
            else
            {
                // single file selected
                android.net.Uri uri = data.getData();
            }
        }
    }
} 

EDIT:

Here is how to properly get all the Uri's:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 10000)
    {
        if (data != null)
        {
            List<Uri> sharedUris = getUrisFromIntent(data);
        }
    }
}

Then the getUrisFromIntent method:

public static List<Uri> getUrisFromIntent(Intent intent) {
    List<Uri> uriList = new ArrayList<>();
    if (intent.getData() != null) {
        uriList.add(intent.getData());
    } else if (intent.getClipData() != null) {
        for (int i = 0; i < intent.getClipData().getItemCount(); i++) {
            uriList.add(intent.getClipData().getItemAt(i).getUri());
        }
    } else {
        if (intent.getAction().equals(Intent.ACTION_SEND)) {
            if (intent.hasExtra(Intent.EXTRA_STREAM)) {
                Uri sharedFile = intent.getParcelableExtra(Intent.EXTRA_STREAM);
                if (sharedFile != null) {
                    uriList.add(sharedFile);
                }
            }
        } else if (intent.getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
            if (intent.hasExtra(Intent.EXTRA_STREAM)) {
                List<Uri> sharedFiles = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
                if (sharedFiles != null) {
                    uriList.addAll(sharedFiles);
                }
            }
        }
    }
    return uriList;
}

This is tested and works on Android API 17 - 29

like image 101
Pierre Avatar answered Oct 21 '25 00:10

Pierre


You are currently specifying /, which is "everything".

As far as I know, there is no mechanism for exclusion in mime types.

You would have to specify specific mime types that you want to allow - there's no way to specify "everything but these two mime types".

like image 27
GreyBeardedGeek Avatar answered Oct 21 '25 00:10

GreyBeardedGeek