1. Download our Official Android App: Forums for Android!

Apps How to delete audio file created using built in sound recorder.

Discussion in 'Android Development' started by KavitaDev, Jun 20, 2011.

  1. KavitaDev

    KavitaDev Newbie
    Thread Starter
    Rank:
    None
    Points:
    35
    Posts:
    13
    Joined:
    Jun 17, 2011

    Jun 17, 2011
    13
    0
    35
    Software Engineer
    India
    Hi All,

    I am newbie to this forum and android as well. Sorry for my weak English.

    I am programming for Android 1.5. In my application, I want to record an audio using inbuilt sound recorder. For this, I am starting sound recorder using action -

    "MediaStore.Audio.Media.RECORD_SOUND_ACTION"

    for an Intent. In onActivityResult(), I had copied the newly created audio file to my custom folder e.g. "/sdcard/myRecordings/sound_1.3gpp".

    Now I want to delete the original file created by sound recorder.

    In DDMS view, I had seen that files were created in a folder -

    "/sdcard/recordingXXXX.3gpp"

    The file name contains XXXX, these are digits that will change every time whenever I am recording audio using sound recorder. I had searched internet, but never found helpful solution for - "how can I delete this file". or "how can I get the location of these files programmatically" (as I am viewing them in DDMS view)

    Someone suggested to use ContentResolver to delete file by specifying Uri returned by sound recorder activity. I had done this using -
    ContentResolver oContentResolver = getContentResolver();
    oContentResolver.delete(oUri, null, null);

    But only Uri entry is deleted and file remains in the folder mentioned above. One suggestion was to start media scanner activity. I had done this using -
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

    I had added appropriate intent filter in manifest file. But, this stops the emulator when I start my application.

    Can anyone give some insight on this? Any help will be greatly appreciated!

    Thanks!
     

    Advertisement

  2. KavitaDev

    KavitaDev Newbie
    Thread Starter
    Rank:
    None
    Points:
    35
    Posts:
    13
    Joined:
    Jun 17, 2011

    Jun 17, 2011
    13
    0
    35
    Software Engineer
    India
    I had metioned that, I had already copied audio file to my custom location and my problem was - "How can I access original file programmatically, so that I can delete it?"

    Can anyone tell me - what I had done below is the efficient way to solve my problem.

    Code (Text):
    1.  
    2. // query uri for MediaStore.Audio.Media.DATA column.
    3.     String[] arrFileDetails = new String[] {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA};
    4.  
    5.     Cursor oCursor = oContentResolver.query(oUri, arrFileDetails, null, null, MediaStore.Audio.Media.DISPLAY_NAME + " DESC"); // returns all file details specified in arrFileDetails array.
    6.  
    7.     oCursor.moveToFirst();
    8.  
    9.     String strData = oCursor.getString(2); // column index 2 - Get the DATA part only.
    10.  
    11.     File oFileToDelete = new File(strData);
    12.  
    13.     if(true == oFileToDelete.delete())
    14.     {
    15.         System.out.println("Original File deleted");
    16.     }
    17.     else
    18.     {
    19.         System.out.println("File can not be deleted");
    20.     }    
    21.     oCursor.close();
    22.     // Delete uri entry from Media DB also.
    23.     int nCount = oContentResolver.delete(oUri, null, null);    
    24.  
    If someone knows more efficient way to achieve the same, I would be thankful for that great help.

    Thanks for your further suggestions.
     

Share This Page

Loading...