TrinityTuts Tips

Set Specific type file using Intent ACTION_GET_CONTENT Android

Hi in this post I can share you code through which you can select specific type file using Intent Action_get_content in Android application.

String[] mimeTypes = {"image/*","application/pdf","application/msword","application/vnd.ms-powerpoint","application/vnd.ms-excel","text/plain"};
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
    if (mimeTypes.length > 0) {
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
    }
} else {
    String mimeTypesStr = "";
    for (String mimeType : mimeTypes) {
        mimeTypesStr += mimeType + "|";
    }
    intent.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
}
startActivityForResult(Intent.createChooser(intent,"ChooseFile"), 510);

Hope this post help you.