如何根据类型搜索文件

时间:2013-05-08 04:58:08

标签: android file search types record

我正在构建一个记录几分钟并保存到app文件夹的应用程序,现在我需要一种方法从文件夹中获取所有.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();
        }
    }
}

2 个答案:

答案 0 :(得分: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( )

答案 1 :(得分: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
}