0

我正在构建一个记录几分钟并保存到应用程序文件夹的应用程序,现在我需要一种方法来从文件夹中获取所有 .3gp 文件并将它们发布到服务器,我不知道如何在 android 中搜索类型文件,搜索了这里的帖子,但没有运气。

这是我用来保存录音的代码,也许你们可以帮我看看...

public void record_file(){

    UserFunctions userFunction = new UserFunctions();

    // Get Global Vars from Database
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    HashMap<String, String> global = db.getGlobalVars();

    id = global.get("id");
    record = global.get("record");
    Log.v("RECORD", "Id: " + id);

    JSONObject json = userFunction.listenVARIABLES(id);

    int duration = Integer.parseInt(record_minutes) * 60 * 1000;


    if (record == "1") {
        try {
    // Save file local to app
            mFileName = path + i + "_record_" + id_ + ".3gp";

            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setMaxDuration(duration);

            try {
                mRecorder.prepare();
            } catch (IOException e) {
                Log.e("AUDIO_RECORDER", "prepare() failed");
            }

            mRecorder.start();

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
4

2 回答 2

2

确保您在清单文件中具有写入权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 File dir =new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");

获取文件的路径

 walkdir(dir);

 ArrayList<String> filepath= new ArrayList<String>();//contains list of all files ending with .3gp

 public void walkdir(File dir) {
 String Pattern3gp = ".3gp";

 File listFile[] = dir.listFiles();

 if (listFile != null) {
 for (int i = 0; i < listFile.length; i++) {
 if (listFile[i].getName().endsWith(Pattern3gp)){
  //Do what ever u want
  filepath.add( listFile[i].getAbsolutePath());
  }
  }  
  }    
  }

获取路径后,您可以将文件上传到服务器

上传视频

   uploadVideo(filepath.get(0));// example of uploading 1st file

使用下面的上传视频

  private void uploadVideo(String videoPath) throws ParseException, IOException {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR_URL);

FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody("Filename: " + videoPath);
StringBody description = new StringBody("This is a description of the video");

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);

// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = response.getEntity( );

// DEBUG
System.out.println( response.getStatusLine( ) );
if (resEntity != null) {
  System.out.println( EntityUtils.toString( resEntity ) );
} // end if

if (resEntity != null) {
  resEntity.consumeContent( );
} // end if

httpclient.getConnectionManager( ).shutdown( );
} // end of uploadVideo( )
于 2013-05-08T05:12:23.540 回答
1

某种解决方案是获取特定文件夹中的文件并解析扩展名为 3gp 的文件。

String path = Environment.getExternalStorageDirectory().toString()+"/yourfolder";
File folder = new File(path);
File filelist[] = folder.listFiles();
ArrayList<File> 3gpfiles = new ArrayList<File>(); // or you can change File to String...
for( File file : filelist )
{
    String fileName = file.getName();
    if( fileName.substring(fileName.length()-4, fileName.length()).equalsIgnoreCase(".3gp") )
        3gpfiles.add(file); // ... and then here change file to fileName
}
于 2013-05-08T05:35:20.507 回答