gpt4 book ai didi

java - Android Intent ACTION_GET_CONTENT 不返回文件扩展名

转载 作者:行者123 更新时间:2023-11-29 02:58:25 25 4
gpt4 key购买 nike

我正在尝试获取用户选择的文件的路径,为结果调用 intent ACTION_GET_CONTENT。

问题是当从文件管理器中选择一个音频文件时, Intent 不返回文件的扩展名(我检查过的文件名中有)。对于视频或图像,它工作正常。

代码如下:

Intent 调用:

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);

关于结果代码:

    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);

获取文件信息函数:

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;
}

因此 strFileName 不包含所选音频文件的扩展名。我也想要音频文件扩展名。

最佳答案

            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();

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

关于java - Android Intent ACTION_GET_CONTENT 不返回文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36710102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com