ACTION_VIEW intent for a file with unknown MimeType

后端 未结 13 1616
抹茶落季
抹茶落季 2020-11-27 10:57

My app has a feature that browse files on your phone and SD card and open them using other apps. I want a solution where I don\'t have to specify the MimeType and can work w

相关标签:
13条回答
  • 2020-11-27 11:34

    This should detect the mimetype and open with default:

    MimeTypeMap myMime = MimeTypeMap.getSingleton();
    Intent newIntent = new Intent(Intent.ACTION_VIEW);
    String mimeType = myMime.getMimeTypeFromExtension(fileExt(getFile()).substring(1));
    newIntent.setDataAndType(Uri.fromFile(getFile()),mimeType);
    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        context.startActivity(newIntent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(context, "No handler for this type of file.", Toast.LENGTH_LONG).show();
    }
    

    Using this function:

    private String fileExt(String url) {
        if (url.indexOf("?") > -1) {
            url = url.substring(0, url.indexOf("?"));
        }
        if (url.lastIndexOf(".") == -1) {
            return null;
        } else {
            String ext = url.substring(url.lastIndexOf(".") + 1);
            if (ext.indexOf("%") > -1) {
                ext = ext.substring(0, ext.indexOf("%"));
            }
            if (ext.indexOf("/") > -1) {
                ext = ext.substring(0, ext.indexOf("/"));
            }
            return ext.toLowerCase();
    
        }
    }
    
    0 讨论(0)
  • 2020-11-27 11:36

    I wrote these few lines in kotlin and and it works like a charm.Enjoy it.

    val file = File(path)              
    val mimeType = URLConnection.guessContentTypeFromName(file.absolutePath)
                    Intent().apply {
                        setDataAndType(Uri.fromFile(file), mimeType)
                        flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    }.let {
                        try {
                            itemView.context.startActivity(it)
                        } catch (e: ActivityNotFoundException) {
                            Toast.makeText(itemView.context, "There's no program to open this file", Toast.LENGTH_LONG).show()
                        }
                    }
    
    0 讨论(0)
  • 2020-11-27 11:39

    You can find out the mime type of a file and make your own intent to open the file.

    Use the following code to open any file...

    File temp_file=new File("YOUR FILE PATH");
    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(temp_file),getMimeType(temp_file.getAbsolutePath()));
    startActivity(intent); 
    

    Where getMimeType() is....(this method will return desired mime type or null if the file does not have any proper mime type)...

    private String getMimeType(String url)
        {
            String parts[]=url.split("\\.");
            String extension=parts[parts.length-1];
            String type = null;
            if (extension != null) {
                MimeTypeMap mime = MimeTypeMap.getSingleton();
                type = mime.getMimeTypeFromExtension(extension);
            }
            return type;
        }
    
    0 讨论(0)
  • 2020-11-27 11:40

    Use this method to get the MIME type from the uri of the file :

    public static String getMimeType(String url) {
    
        String type = null;
        String extension = url.substring(url.lastIndexOf(".") + 1);
        if (extension != null) {
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        }
        return type;
    }
    
    0 讨论(0)
  • 2020-11-27 11:42

    You should always check the same with

    PackageManager packageManager = getActivity().getPackageManager();
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent);
    } else {
        Log.d(TAG, "No Intent available to handle action");
    
    }
    

    and to get MimeTpye of file you can use :-

     public String getMimeType(Uri uri, Context context) {
            String mimeType = null;
            if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
                ContentResolver cr = context.getContentResolver();
                mimeType = cr.getType(uri);
            } else {
                String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
                        .toString());
                mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                        fileExtension.toLowerCase());
            }
            return mimeType;
        }
    
    0 讨论(0)
  • 2020-11-27 11:45

    Android does not start activities based on file extensions, unless there's an app that specifies a particular intent filter for it. You will need the mime type to the Intent to tell android enough information to start the right Activity.

    You have the option to automate this task by using the MimeTypeMap class. Check the method String getMimeTypeFromExtension(String extension).

    BTW, do you have a pdf reader installed in your device?

    You should also handle this exception, probably by showing a nice popup saying that there is no app for that particular type of file.

    0 讨论(0)
提交回复
热议问题