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 3965140
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:25:21+00:00 2026-05-20T03:25:21+00:00

I need to get the full path to a file somewhere on the phone

  • 0

I need to get the full path to a file somewhere on the phone (any location) and play it with MediaPlayer.

ive heard of using a file chooser for Android (by launching an intent).

In the test code, I just copied a resource to a another file, got the path and passed it to AudioVideoEntry (as i show later, a very simple and thin wrapper around MediaPlayer)

Here’s the test code I’ve written:

private String ave_path;
    private String ave_file_name = "my_media_content";
    private InputStream ave_fis;
    private OutputStream ave_fos;
    public void testAudioVideoEntry()
    {
        //get the Activity
        Module_JournalEntry journalentryactivity = getActivity();
        //open an InputStream to a resource file (in this case strokes.mp3)
        ave_fis = journalentryactivity.getResources().openRawResource(module.jakway.JournalEntry.R.raw.strokes);

        //open an OutputStream to a new file
        try {
            ave_fos = journalentryactivity.openFileOutput(ave_file_name, 
                                                Context.MODE_PRIVATE);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            assertTrue(false);
        }
        catch(Exception e)
        {
            assertTrue(false);
        }

        //copy the data from the resource into
        //the OutputStream
        int data;
        try {
        while((data = ave_fis.read()) != -1)
        {
            ave_fos.write(data);
        }
            assertTrue(true);
        }
        catch(Exception e)
        {
            assertTrue(false);
        }

        //get the full path of the file we wrote to
        ave_path = journalentryactivity.getFileStreamPath(ave_file_name).toString();

        //and construct a new object of AudioVideoEntry with that path
        AudioVideoEntry ave = new AudioVideoEntry(ave_path);

        //register an error listener via MediaPlayer's setOnErrorListener
        ave.setOnErrorListener(new OnErrorListener()
                                {
                                    @Override
                                    public boolean onError(MediaPlayer mp,
                                            int what, int extra) {
                                        Log.e("MEDIAPLAYER ERRORS",
                                        "what: " + what + "  extra: "   + extra);
                                        assertTrue(false);
                                        // TODO Auto-generated method stub
                                        return false;
                                    }
                                });
        ave.prepareMedia();
        ave.playMedia();
        try {
        ave_fis.close();
        ave_fos.close();
        }
        catch(Exception e)
        {
            assertTrue(false);
            e.printStackTrace();
        }

AudioVideoEntry is basically a thin wrapper around MediaPlayer that can hold its own path:

public class AudioVideoEntry
{
    private String path_to_audio_file;
    private MediaPlayer mediaplayer;

    /**
     * Initialize the internal MediaPlayer
     * from the String parameter
     * @param set_path_to_audio_file
     */
    public AudioVideoEntry(String set_path_to_audio_file)
    {
        path_to_audio_file = set_path_to_audio_file;
        mediaplayer = new MediaPlayer();
        try {
            mediaplayer.setDataSource(path_to_audio_file);
            mediaplayer.prepare();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public AudioVideoEntry(FileDescriptor fd)
    {

        mediaplayer = new MediaPlayer();
        try {
            mediaplayer.setDataSource(fd);
            mediaplayer.prepare();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }



    /**
     * Begin playing media
     */
    public void prepareMedia()
    {
        try {
            mediaplayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * play media
     * don't forget to prepare() if necessary
     */
    public void playMedia()
    {
        mediaplayer.start();
    }

    /**
     * pause the media
     * can be played later
     */
    public void pauseMedia()
    {
        mediaplayer.pause();
    }

    /**
     * stop media
     */
    public void stopMedia()
    {
        mediaplayer.stop();
    }

    public void setOnErrorListener(OnErrorListener listener)
    {
        mediaplayer.setOnErrorListener(listener);
    }
}

here’s the logcat output from the JUnit test (the tests were successful, the actual results – as the logat shows – were not)

02-07 09:40:23.129: ERROR/MediaPlayer(1209): error (1, -2147483648)
02-07 09:40:23.139: WARN/System.err(1209): java.io.IOException: Prepare failed.: status=0x1
02-07 09:40:23.149: WARN/System.err(1209):     at android.media.MediaPlayer.prepare(Native Method)
02-07 09:40:23.149: WARN/System.err(1209):     at module.jakway.JournalEntry.AudioVideoEntry.<init>(AudioVideoEntry.java:39)
02-07 09:40:23.149: WARN/System.err(1209):     at module.jakway.JournalEntry.test.Module_JournalEntryTest.testAudioVideoEntry(Module_JournalEntryTest.java:182)
02-07 09:40:23.149: WARN/System.err(1209):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 09:40:23.149: WARN/System.err(1209):     at java.lang.reflect.Method.invoke(Method.java:507)
02-07 09:40:23.159: WARN/System.err(1209):     at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
02-07 09:40:23.159: WARN/System.err(1209):     at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
02-07 09:40:23.159: WARN/System.err(1209):     at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
02-07 09:40:23.159: WARN/System.err(1209):     at junit.framework.TestCase.runBare(TestCase.java:127)
02-07 09:40:23.169: WARN/System.err(1209):     at junit.framework.TestResult$1.protect(TestResult.java:106)
02-07 09:40:23.169: WARN/System.err(1209):     at junit.framework.TestResult.runProtected(TestResult.java:124)
02-07 09:40:23.169: WARN/System.err(1209):     at junit.framework.TestResult.run(TestResult.java:109)
02-07 09:40:23.179: WARN/System.err(1209):     at junit.framework.TestCase.run(TestCase.java:118)
02-07 09:40:23.179: WARN/System.err(1209):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
02-07 09:40:23.179: WARN/System.err(1209):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
02-07 09:40:23.179: WARN/System.err(1209):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
02-07 09:40:23.189: WARN/System.err(1209):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
02-07 09:40:23.189: ERROR/MediaPlayer(1209): prepareAsync called in state 0
02-07 09:40:23.189: WARN/System.err(1209): java.lang.IllegalStateException
02-07 09:40:23.189: WARN/System.err(1209):     at android.media.MediaPlayer.prepare(Native Method)
02-07 09:40:23.189: WARN/System.err(1209):     at module.jakway.JournalEntry.AudioVideoEntry.prepareMedia(AudioVideoEntry.java:79)
02-07 09:40:23.199: WARN/System.err(1209):     at module.jakway.JournalEntry.test.Module_JournalEntryTest.testAudioVideoEntry(Module_JournalEntryTest.java:197)
02-07 09:40:23.199: WARN/System.err(1209):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 09:40:23.199: WARN/System.err(1209):     at java.lang.reflect.Method.invoke(Method.java:507)
02-07 09:40:23.199: WARN/System.err(1209):     at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
02-07 09:40:23.199: WARN/System.err(1209):     at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
02-07 09:40:23.199: WARN/System.err(1209):     at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
02-07 09:40:23.199: WARN/System.err(1209):     at junit.framework.TestCase.runBare(TestCase.java:127)
02-07 09:40:23.199: WARN/System.err(1209):     at junit.framework.TestResult$1.protect(TestResult.java:106)
02-07 09:40:23.199: WARN/System.err(1209):     at junit.framework.TestResult.runProtected(TestResult.java:124)
02-07 09:40:23.199: WARN/System.err(1209):     at junit.framework.TestResult.run(TestResult.java:109)
02-07 09:40:23.199: WARN/System.err(1209):     at junit.framework.TestCase.run(TestCase.java:118)
02-07 09:40:23.199: WARN/System.err(1209):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
02-07 09:40:23.199: WARN/System.err(1209):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
02-07 09:40:23.199: WARN/System.err(1209):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
02-07 09:40:23.199: WARN/System.err(1209):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
02-07 09:40:23.199: ERROR/MediaPlayer(1209): start called in state 0
02-07 09:40:23.199: ERROR/MediaPlayer(1209): error (-38, 0)

Edit: why is MediaPlayer failing?

thanks!
dragonwrenn

  • 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-20T03:25:22+00:00Added an answer on May 20, 2026 at 3:25 am

    The second call to MediaPlayer.prepare() (already called once in the AudioVideoEntry ctor) in the AudioVideoEntry.prepareMedia() method was easy to spot as other people have noticed.

    The harder error to find was the first error.

    I used an Ogg file for testing.

    First clue was from davidsparks reply (last one) in android-platform – Ogg on G1

    As long as the files have a .ogg extension, they should play with the
    built-in music player. We rely on the file extensions because there is
    no centralized file recognizer for the media scanner.

    Second clue was from [android-developers] Re: File permission about MediaPlayer

    Due to the Android security model, MediaPlayer does not have root
    access rights. It can access the sdcard, but it can’t access private
    app directories.

    Your app can explicitly grant MediaPlayer temporary access to secure
    files by opening the file and passing the file descriptor to
    MediaPlayer using the setDataSource(FileDescriptor fd) method.

    If you look at the absolute path of the output stream, you see it’s in the /data/data directory under the app’s package name’s directory.

    Excuse the timestamps – I worked backwards to get data to show on a OS2.1update1 (API7) emulator.

    Your code had:

    String ave_file_name = "my_media_content";
    
    ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_PRIVATE);
    

    DDMS showed:

    02-10 05:10:28.253: WARN/MediaPlayer(1992): info/warning (1, 26)
    02-10 05:10:28.253: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
    02-10 05:10:28.253: ERROR/MediaPlayer(1992): error (1, -4)
    02-10 05:10:28.274: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete
    

    If we change JUST the file to MODE_WORLD_READABLE:

    String ave_file_name = "my_media_content";
    
    ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_WORLD_READABLE);
    

    DDMS shows no improvement:

    02-10 05:08:28.543: WARN/MediaPlayer(1900): info/warning (1, 26)
    02-10 05:08:28.553: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
    02-10 05:08:28.553: ERROR/MediaPlayer(1900): error (1, -4)
    02-10 05:08:28.563: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete
    

    If we change JUST the file extension to ogg:

    String ave_file_name = "my_media_content.ogg";
    
    ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_PRIVATE);
    

    We get a change in DDMS output:

    02-10 04:59:30.153: ERROR/MediaPlayerService(31):   error: -2
    02-10 04:59:30.163: ERROR/MediaPlayer(1603): Unable to to create media player
    

    But when we combine the two:

    String ave_file_name = "my_media_content.ogg";
    
    ave_fos = activity.openFileOutput(ave_file_name, Context.MODE_WORLD_READABLE);
    

    DDMS shows no errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the name of a file from its full path, in
I need to get the full path of the file that I'm editing with
I have found a solution to get the full file path of a file
I need to get an absolute file path into the native AIR browser. I've
I need to get the full height of a UITableView (i.e. the height at
I need to get the full domain in which the user sent the ajax
I have an HTML form POSTing to the following index.php: <?php require_once(/home/full/path/to/included/file.php); ?> And
While upload a file i will get the tmp_name in the array, I need
VS 2008 How to fetch the Full File path for the File Uploaded in
Is there any way by which we can get the path of the source

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.