Riddhi Daftary
Riddhi Daftary

Reputation: 301

select only specific file type using intent.getContent method

I am trying to select some files from the device on specific event using following code.

For Audio i used:

intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Select ,Music"),AUDIO_REQUEST);

But Problem is when user tries to select from es file explorer it also allows other file types to be selected. How can I prevent other file types to be selected and allow only audio types. The screendshot is here. Screenshot

Edit

I also wanted to select document file type such as doc ppt xl etc. for that I have used

String[] mimeTypes = {"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
                            "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
                            "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
                            "text/plain",
                            "application/pdf"};

intent.setAction(Intent.ACTION_GET_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
} else {
       String mimeTypesStr = "";
        for (String mimeType : mimeTypes) {
                 mimeTypesStr += mimeType + "|";
        }
       intent.setType(mimeTypesStr.substring(0, mimeTypesStr.length() - 1));
}
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "select Document"),DOCUMENT_REQUEST);

But, Same thing with happened to this also. Can Anyone please help me with this problem?

Upvotes: 0

Views: 2193

Answers (1)

Anil
Anil

Reputation: 1615

Try

intent.setType("audio/mpeg");

or

intent.setType("audio/mp3");

or try like this

Intent intent = null;
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
         intent = new Intent(Intent.ACTION_GET_CONTENT);
    }else {
         intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    }

    // The MIME data type filter
    intent.setType("audio/*")

Upvotes: 3

Related Questions