Search code examples
androidfilesearchtypesrecord

How to search files based on type


I'm building an app that records for a few minutes and saves to app folder, now i need a way lo get all .3gp files from folder and post them to server, i have no clue how to search for type files in android, searched the posts in here but no luck.

Here's the code i use to save the recordings, maybe you guys can give me a hand here...

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

Solution

  • Make sure you have write permission in manifest file

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
     File dir =new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");
    

    To get the path of the files

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

    Once you get the path you can upload files to the server

    To upload video

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

    Use the below to upload video

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