使用按钮我想通过我的服务器从手机外部存储目录将音频上传到soundcloud。如何从外部存储目录中获取歌曲。我将如何从外部存储获取路径并使用http post上传到服务器。
我想查看mp3,mp4,amr audios的格式。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new GinfyDbAdapter(this);
setContentView(R.layout.upload_audiogallery);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
upload = (Button) findViewById(R.id.btnuplaod);
btnstop = (Button) findViewById(R.id.btnstop);
//Bundle extras = getIntent().getExtras();
mRowId = savedInstanceState != null ? savedInstanceState.getLong(GinfyDbAdapter.CATEGORY_COLUMN_ID4)
: null;
registerButtonListenersAndSetDefaultText();
//for speech to text and recording purpose
setButtonHandlers();
enableButtons(false);
mp = new MediaPlayer();
}
private void registerButtonListenersAndSetDefaultText() {
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setResult(RESULT_OK);
Toast.makeText(Uploadaudiogallery.this, getString(R.string.task_saved_message), Toast.LENGTH_SHORT).show();
finish();
}
});
upload .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFromSdcard();
}
public void getFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory().getPath());
if (file.isDirectory())
{
listFile = file.listFiles();
String[] Patternmp3 = {".3gp",".mp3",".amr",".mp4" };
for (int i = 0; i < listFile.length; i++)
{
if (listFile[i].getName().endsWith(Patternmp3)){
f.add(listFile[i].getAbsolutePath());
}
}
}
}
});
}
如何获取格式的音频文件,以及如何获取发送到服务器上传的路径。
答案 0 :(得分:1)
要上传歌曲,您需要获取外部存储目录中音频文件的路径
String[] extensions = { ".mp3",".mp4",".MP3",".MP4"};
然后
String rootpath = Environment.getExternalStorageDirectory().getAbsolutePath();
// get the path of external storage directory
getAllAudioFilePath(rootpath);
然后遍历文件夹并获取与扩展名匹配的文件的路径
private void getAllAudioFilePath(String rootFolder) {
File file = new File(rootFolder);
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (f.isDirectory()) {
loadAllImages(f.getAbsolutePath());
} else {
for (int i = 0; i < extensions.length; i++) {
if (f.getAbsolutePath().endsWith(extensions[i])) {
Log.i("Song files paths....",""+f.getAbsolutePath());
}
}
}
}
}
}
}
获得文件的路径后,您可以通过执行http帖子将其上传到服务器。
另外,不要忘记在清单文件中添加读取权限