How to open file with any exetension in Android?

前端 未结 3 771
走了就别回头了
走了就别回头了 2021-01-12 09:22

I have an app in which user can upload file with any exetention(.png,.mp3,.txt,.pdf

3条回答
  •  無奈伤痛
    2021-01-12 09:54

    The following code will work for every file type:

    try {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), fileMimeType);
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        // no Activity to handle this kind of files
    }
    

    Of course, there should be an Activity in the system that knows how to handle the file of a certain type. Hope this helps.

提交回复
热议问题