我想要获取我选择的实际音频文件的句柄。为了选择音频文件,我的意图如下:
Intent audioIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(audioIntent , 0);
它允许我从弹出窗口中选择音频文件但是如何获取我选择的文件?
对于图像,我有:
bitmap = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), intent.getData());
音频文件有类似内容吗?
答案 0 :(得分:5)
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, AUDIO_GALLERY_REQUEST_CODE);
if (requestCode == AUDIO_GALLERY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
try {
String path = _getRealPathFromURI(context, intent.getData());
File audio = new File(path);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String _getRealPathFromURI(Context context, Uri contentUri) {
String[] proj = { MediaStore.Audio.Media.DATA };
CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
答案 1 :(得分:0)
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
应该有类似的方法,它有getContentUri(String)
,返回Uri
个对象。看看是否有帮助。