0

使用一个按钮,我想通过我的服务器从我的手机外部存储目录将音频上传到 soundcloud。如何从外部存储目录中获取歌曲。我将如何从外部存储获取路径并使用 http post 上传到服务器。

我想检查 mp3、mp4、amr 音频的格式。

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

                    }

                }
        }
  });
  }

如何获取格式的音频文件以及如何获取发送到服务器上传的路径。

4

1 回答 1

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 post 来使用它来上传到服务器。

也不要忘记在清单文件中添加读取权限

于 2013-10-24T12:30:51.807 回答