search

Home  >  Q&A  >  body text

java - 上传文件功能,选择本地文件action

最近在做一个即时通讯,选择文件上传功能.
当我点击按钮选择要上传的文件时的action,使用

 Intent i=new Intent();
        i.setAction(Intent.ACTION_GET_CONTENT);
        i.setType("*/*");
        startActivityForResult(i,20);
        
    这样可以打开文件管理器,但是一直很不懂setType
    setType("image/*");  是打开图片文件
    setType(“audio/*”); 是打开音频
    问题是:
    setType("*/*");  是打开所有类型的文件吗?
    setType("file/*");  要怎么理解?
    
    在onActivityResult返回值中我需要获取到点击文件的路径用于上传文件,如何获取?,希望有大神指点指点
       
阿神阿神2822 days ago532

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 09:19:27

    For what value to set for setType, you have to look at Android MIME first, and you will know what type should be passed accordingly. There is no "file/" type in MIME. "*/*" refers to all MIME files. The return value of onActivityResult contains Intent data, through which you can obtain the file path you selected.

    Uri uri = data.getData();
    String path = uri.getPath();

    reply
    0
  • Cancelreply