Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6211607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:22:07+00:00 2026-05-24T06:22:07+00:00

I am trying to invoke the audio recorder on Android 2.2.1 (device Samsung Galaxy

  • 0

I am trying to invoke the audio recorder on Android 2.2.1 (device Samsung Galaxy POP) using the following code:

private static final int ACTIVITY_RECORD_SOUND = 1;
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, ACTIVITY_RECORD_SOUND);

This invokes the recorder successfully. In my activity result i do the following:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
                         case ACTIVITY_RECORD_SOUND:
                             data.getDataString();
                         break;
                            }
                }
       }

After i complete the recording i press back on the audio recorder which returns the control to the onActivityResult method as expected, but my resultCode is always 0 (which is Activity.RESULT_CANCELED) and my data is null. Am i missing out on something here? Kindly help me with this. This works on the emulator but not on the device. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:22:08+00:00Added an answer on May 24, 2026 at 6:22 am

    I finally found a workaround for my problem by using the FileObserver. I achieved it by doing the following:

    package com.pravaa.audiointent;
    
     import java.io.DataOutputStream;
     import java.io.File;
     import java.io.FileInputStream;
     import java.io.FileNotFoundException;
     import java.io.FileOutputStream;
     import java.io.IOException;
     import java.util.Enumeration;
     import java.util.Vector;
    
     import android.app.Activity;
     import android.content.Intent;
     import android.os.Bundle;
     import android.os.FileObserver;
     import android.provider.MediaStore;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.widget.Button;
     import android.widget.LinearLayout;
     import android.widget.Toast;
    
    public class AudioActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    private Button sampleButton;
    private FileObserver mFileObserver;
    private Vector<String> audioFileNames;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        audioFileNames = new Vector<String>();
        LinearLayout finalContainer = new LinearLayout(this);
        sampleButton = new Button(this);
        sampleButton.setOnClickListener(this);
        sampleButton.setText("Start Audio Intent");
        finalContainer.addView(sampleButton);
        setContentView(finalContainer);
        addObserver();
    
    }
    
    private void addObserver() {
        this.mFileObserver = new FileObserver("/sdcard/Sounds/") {
            @Override
            public void onEvent(int event, String path) {
                if (event == FileObserver.CREATE) {
                    if (path != null) {
                        int index = path.indexOf("tmp");
                        String tempFileName = (String) path.subSequence(0,
                                index - 1);
                        audioFileNames.add(tempFileName);
    
                    }
                } else if (event == FileObserver.DELETE) {
                    if (path != null) {
                        int index = path.indexOf("tmp");
                        String tempFileName = (String) path.subSequence(0,
                                index - 1);
                        if (audioFileNames.contains(tempFileName)) {
                            audioFileNames.remove(tempFileName);
                        }
                    }
    
                }
            }
        };
    }
    
    private void readFile(String fileName) {
    
        File attachment = new File("/sdcard/Sounds/" + fileName);
        if (attachment.exists()) {
            FileInputStream fis;
            try {
                fis = new FileInputStream(attachment);
                byte[] bytes = new byte[(int) attachment.length()];
                try {
                    fis.read(bytes);
                    fis.close();
    
                    attachment.delete();
    
                    saveMedia("Test" + fileName, bytes);
    
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mFileObserver.startWatching();
    }
    
    public void saveMedia(String fileName, byte[] data) {
    
        String imagePath = "/sdcard/sam/";
        System.out.println("Inside Folder");
    
    
        File file = new File(imagePath, fileName);
        System.out.println("File Created");
    
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            DataOutputStream dataOutputStream = new DataOutputStream(
                    fileOutputStream);
            System.out.println("Writting File");
            dataOutputStream.write(data, 0, data.length);
            System.out.println("Finished writting File");
            dataOutputStream.flush();
            dataOutputStream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
        startActivityForResult(intent, 2); 
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
        // TODO Auto-generated method stub
        if (requestCode == 2) {
            if (mFileObserver != null) {
                mFileObserver.stopWatching();
            }
            Enumeration<String> audioFileEnum = audioFileNames.elements();
            while (audioFileEnum.hasMoreElements()) {
                readFile((String) audioFileEnum.nextElement());
            }
        }
    }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to invoke addEventListener() using apply() method. The code is like: function rewrite(old){
I am trying to invoke Invoke on a Dispatcher using the following overload: public
I am trying to invoke sendmail via PHP's mail function by the following code:
I was trying to invoke a webservice from phonegap android app using jQuery .ajax
I am trying to invoke the following macro in my .cpp file: #define IAP_ROM_LOCATION
I am trying to invoke a WebService through SOAP using Erlang and YAWS (yaws_soap_lib
I am trying to invoke camera from HTML5 and JavaScript. My code is as
I'm trying to invoke some C++ code in case a trigger is called inside
I have been trying to invoke my application from browser on android 3.1 (Honeycomb)
I'm currently trying to invoke a method made in C from C# C code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.