I am trying get the path of the file selected by user, using calling the intent ACTION_GET_CONTENT for result.
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:
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. 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/
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
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.