Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 操作获取内容:搜索多个文件类型时如何获取文件结尾?_Android_Android Intent_Sd Card_Filepath - Fatal编程技术网

Android 操作获取内容:搜索多个文件类型时如何获取文件结尾?

Android 操作获取内容:搜索多个文件类型时如何获取文件结尾?,android,android-intent,sd-card,filepath,Android,Android Intent,Sd Card,Filepath,我使用的意图是让用户选择一个文件,在用户选择后,我想知道所选文件是什么类型的文件 目的是: Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); 在onActivityResult中,我想通过intent.getData()及其所有“子方法”(getScheme()、getLastPathSegment()等)从intent中提取路径。但是,我只获取所选文件的Uri URI'的示例: Uri:

我使用的意图是让用户选择一个文件,在用户选择后,我想知道所选文件是什么类型的文件

目的是:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
在onActivityResult中,我想通过intent.getData()及其所有“子方法”(getScheme()、getLastPathSegment()等)从intent中提取路径。但是,我只获取所选文件的Uri

URI'的示例:

Uri: content://media/external/audio/media/15185 //This is an audiofile
Uri: content://media/external/audio/media/20 //Another audiofile
Uri: content://media/external/images/media/20577 //this is a picture
Uri: file:///storage/emulated/0/testWed%20Apr%2017%2011%3A10%3A34%20CEST%202013 //This is a file
我看到了当用户只能选择图像或音频时,如何获得绝对路径的解决方案。但是,如果我想允许用户从不同的文件类型中进行选择,如何获得绝对路径(带有名称和文件结尾的真实路径,例如MyPicture.jpeg)

我一直在使用的代码试图在onActivityResult(int requestCode,int resultCode)中获取路径名

         String fileName = data.getData().getLastPathSegment().toString();
         System.out.println("Uri: " +data.getData().toString());
         File f = new File(fileName);
         System.out.println("TrYinG wIWThA Da FiLE: " +f.getAbsolutePath());
         System.out.println("FileNAME!!!: "+fileName);

关于文件结尾,我假设您指的是文件扩展名?(即mp4)

如果是这样,您可以使用
ContentResolver
获取mimetype:

String mimeType = getContentResolver().getType(sourceUri);
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
这将为您提供mimetype,假设它们位于
MediaStore
中,如果您有内容
Uri
,则它们应该位于该位置。因此,您将获得类似“image/png”、“video/mp4”等内容

然后可以使用
MimeTypeMap
从mimetype获取文件扩展名:

String mimeType = getContentResolver().getType(sourceUri);
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
请注意,决不应在“/”之后将mimetype子串以获取扩展名,因为某些mimetype不使用其扩展名,例如具有mimetype“video/quicktime”的“.mov”文件