简体   繁体   English

Android Intent ACTION_GET_CONTENT 不返回文件扩展名

[英]Android Intent ACTION_GET_CONTENT does not return extension of file

I am trying get the path of the file selected by user, using calling the intent ACTION_GET_CONTENT for result.我正在尝试获取用户选择的文件的路径,使用调用意图 ACTION_GET_CONTENT 获取结果。

The problem is when Selecting an audio file from the file manager, the intent does not return the extension of the file (which is there in the file name i checked it).问题是从文件管理器中选择音频文件时,意图不会返回文件的扩展名(我检查过的文件名中有)。 In the case of video or image it is working fine.在视频或图像的情况下,它工作正常。

Here is the code:这是代码:

Intent Calling:意图调用:

Intent intent = getIntent();
if(intent.getAction() == null) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    else
        intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"),CloudConstants.CLOUD_REQUEST_FILE_CHOOSER);

On Result Code:在结果代码上:

    if (data != null) {
    //Get URI Data from Intent - URI is of the file chosen by the User in the
    //File picker
    uriFileURI = data.getData();
    if(uriFileURI != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    final int intFlags = data.getFlags()&(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(data.getData(), intFlags);
}
    //Check if URI was returned or not; NULL is returned if file was chosen from
    //via gallery share option
    //In such a case, the URI is retrieved from ClipData object of the Intent
    if (uriFileURI == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && data.getClipData().getItemCount() > 0)
        uriFileURI = data.getClipData().getItemAt(0).getUri();
    //Log File URI
    Log.i("CloudMedia", "File Uri: " + String.valueOf(uriFileURI));
    //Generate Absolute File name to publish on Title
    strFileName = getFileInformation(uriFileURI, MediaStore.Files.FileColumns.DISPLAY_NAME);

getFileInformation Function: getFileInformation 函数:

public String getFileInformation(Uri strFileURI, String strProjection) {
        Cursor cursorFileId = getContentResolver().query(strFileURI,
                new String[] {
                        strProjection
                }, null, null, null);
        if(cursorFileId.moveToFirst()) {
            return cursorFileId.getString(cursorFileId.getColumnIndex(strProjection));
        } else
            return null;
    }

So the strFileName does not contain the extension of the audio file selected.因此strFileName不包含所选音频文件的扩展名。 I want the audio file extension also.我也想要音频文件扩展名。

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String imgFullPath = cursor.getString(columnIndex);
            cursor.close();//String file = uri.toString();

source http://programmerguru.com/android-tutorial/how-to-pick-image-from-gallery/来源http://programmerguru.com/android-tutorial/how-to-pick-image-from-gallery/

Try this code.试试这个代码。 I used android.provider.OpenableColumns.DISPLAY_NAME instead of MediaStore.Files.FileColumns.DISPLAY_NAME and set null for projection in getContentResolver().query我使用 android.provider.OpenableColumns.DISPLAY_NAME 而不是 MediaStore.Files.FileColumns.DISPLAY_NAME 并在 getContentResolver().query 中为投影设置 null

strFileName = getFileInformation(uriFileURI, android.provider.OpenableColumns.DISPLAY_NAME);

public String getFileInformation(Uri strFileURI, String strProjection) {
    Cursor cursorFileId = getContentResolver().query(strFileURI,
            null, null, null, null);
    if(cursorFileId.moveToFirst()) {
        return cursorFileId.getString(cursorFileId.getColumnIndex(strProjection));
    } else
        return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从ACTION_GET_CONTENT Intent获取的URI中获取文件名? - How to get the file name from an URI got from ACTION_GET_CONTENT Intent? OnActivityResult 有时不会在 ACTION_GET_CONTENT 意图后调用 - OnActivityResult sometimes not called after ACTION_GET_CONTENT intent ACTION_GET_CONTENT给出错误的路径 - ACTION_GET_CONTENT gives wrong path ACTION_GET_CONTENT 只有文件,没有图片或视频 - ACTION_GET_CONTENT only files, no images or videos android.content.ActivityNotFoundException:未找到任何处理Intent的活动{act = android.intent.action.GET_CONTENT - android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT 如何获取使用 Intent.ACTION_OPEN_DOCUMENT 选择的文件的文件扩展名? - How to get file extension of a file picked with Intent.ACTION_OPEN_DOCUMENT? 使用Android Intent共享文件:删除文件扩展名 - Sharing a file with Android Intent: File extension is removed Android:如何从Intent.ACTION_GET_CONTENT获得的URI中检索视频缩略图? - Android: How to retrieve video Thumbnail from URI obtained from Intent.ACTION_GET_CONTENT? 如何从 Intent.ACTION_GET_CONTENT 获取完整路径? - How to get full path from Intent.ACTION_GET_CONTENT? Android Intent B是否不会返回Intent A的下一行? - Android Intent B does not return to next line of Intent A?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM