我怎样才能音频文件,用户挑选 [英] How can i get the Audio file that the user picks
本文介绍了我怎样才能音频文件,用户挑选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望得到一个句柄,我挑实际的音频文件。对于选择音频文件,我有一个看起来像这样的意图:
意图audioIntent =新意图(Intent.ACTION_PICK,android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(audioIntent,0);
它让我得到一个弹出来接音频文件从,但我怎样才能得到我选择的文件?
在图像的情况下,我有:
=位android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(),intent.getData());
是否有音频文件相似的地方?
解决方案
意图galleryIntent =新意图(Intent.ACTION_PICK,android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent,AUDIO_GALLERY_REQUEST_ code);如果(要求code == AUDIO_GALLERY_REQUEST_ code){
如果(结果code == Activity.RESULT_OK){
尝试{
字符串路径= _getRealPathFromURI(背景下,intent.getData());
文件中的音频=新的文件(路径);
}赶上(例外五){
e.printStackTrace();
}
}
}私人字符串_getRealPathFromURI(上下文的背景下,乌里contentUri){
的String [] = PROJ {MediaStore.Audio.Media.DATA};
CursorLoader装载机=新CursorLoader(背景下,contentUri,凸出,NULL,NULL,NULL);
光标光标= loader.loadInBackground();
INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(Column_Index中);
}
I would want to get a handle to the actual audio file that i pick. For selecting the audio file, i have an intent that looks like this :
Intent audioIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(audioIntent , 0);
It allows me to get a popup to pick audio files from but how can i get the file that i have picked ?
In the case of images, i have :
bitmap = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), intent.getData());
Is there something similar for audio files ?
解决方案
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);
}
这篇关于我怎样才能音频文件,用户挑选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文