0

Just got my Nexus 7 and when I play an audio file I get this error, then a force close. I've been searching all day for a solution. Here's the relevant code:

protected void onListItemClick(ListView l, View v, int position, long id) {
playSong(MEDIA_PATH + songs.get(position));
    textViewRecordPlaying.setText(songs.get(position));
}

private void playSong(String songPath) {
   final Intent i = new Intent(MusicService.ACTION_URL);
   File file = new File(songPath);
   Uri uri = Uri.fromFile(file);
   i.setData(uri);
   startService(i);

   }

playSong(MEDIA_PATH + songs.get(position));

My MEDIA_PATH:

private static final String MEDIA_PATH = new String(Environment
      .getExternalStorageDirectory().getPath() + "/AudioStreamRecorder/");

This code works beautifully on my Galaxy S3 and my Thunderbolt, but when I click on a list item I get Failed to open file. Permission denied. error on Nexus 7 and the Asus Transformer Prime TF201, then a force close.

I have tried Environment.getExternalStorageDirectory(); and Environment.getExternalStorageDirectory()getAbsolutePath(); with the same errors. I have declared <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> and <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> Someone please help I have run out of ideas...

05-07 16:47:35.714: I/Streaming Radio Recorder Pro(4427): debug: Creating service
05-07 16:47:35.724: I/MusicRetriever(4427): Querying media...
05-07 16:47:35.724: I/MusicRetriever(4427): URI: content://media/external/audio/media
05-07 16:47:35.724: I/Streaming Radio Recorder Pro(4427): Playing from URL/path: file:///storage/emulated/0/AudioStreamRecorder/The%20Dining%20Rooms%20-%20triste%2C%20solitario%20y%20final-3.54.44%20Tuesday%2C%20May%207%2C%202013.mp3
05-07 16:47:35.724: I/AudioService(436):  AudioFocus  requestAudioFocus() from android.media.AudioManager@42ffe928com.LudwigAppDesign.streamingradioplayerpro.AudioFocusHelper@42ffe2b8
05-07 16:47:35.724: I/AwesomePlayer(128): setDataSource_l(URL suppressed)
05-07 16:47:35.724: I/MusicRetriever(4427): Query finished. Returned a cursor.
05-07 16:47:35.724: I/MusicRetriever(4427): Listing...
05-07 16:47:35.724: I/MusicRetriever(4427): Title column index: 8
05-07 16:47:35.724: I/MusicRetriever(4427): ID column index: 8
05-07 16:47:35.724: I/MusicRetriever(4427): ID: 21 Title: Join Hangout
05-07 16:47:35.724: I/MusicRetriever(4427): ID: 2305 Title: The Dining Rooms - triste, solitario y final-3.54.44 Tuesday, May 7, 2013
05-07 16:47:35.724: I/MusicRetriever(4427): Done querying media. MusicRetriever is ready.
05-07 16:47:35.734: E/(128): Failed to open file '/storage/emulated/0/AudioStreamRecorder/The Dining Rooms - triste, solitario y final-3.54.44 Tuesday, May 7, 2013.mp3'. (Permission denied)
05-07 16:47:35.734: E/MediaPlayer(4427): error (1, -2147483648)
05-07 16:47:35.734: E/MediaPlayer(4427): Error (1,-2147483648)
05-07 16:47:35.734: E/Streaming Radio Recorder Pro(4427): Error: what=1, extra=-2147483648
05-07 16:47:35.764: I/AudioService(436):  AudioFocus  abandonAudioFocus() from android.media.AudioManager@42ffe928com.LudwigAppDesign.streamingradioplayerpro.AudioFocusHelper@42ffe2b8
05-07 16:47:36.704: D/AndroidRuntime(4427): Shutting down VM
05-07 16:47:36.704: W/dalvikvm(4427): threadid=1: thread exiting with uncaught exception (group=0x41f66930)
05-07 16:47:36.714: E/AndroidRuntime(4427): FATAL EXCEPTION: main
05-07 16:47:36.714: E/AndroidRuntime(4427): java.lang.NullPointerException
05-07 16:47:36.714: E/AndroidRuntime(4427):     at com.LudwigAppDesign.streamingradioplayerpro.Songs$8.run(Songs.java:407)
05-07 16:47:36.714: E/AndroidRuntime(4427):     at android.os.Handler.handleCallback(Handler.java:725)
05-07 16:47:36.714: E/AndroidRuntime(4427):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 16:47:36.714: E/AndroidRuntime(4427):     at android.os.Looper.loop(Looper.java:137)
05-07 16:47:36.714: E/AndroidRuntime(4427):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-07 16:47:36.714: E/AndroidRuntime(4427):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 16:47:36.714: E/AndroidRuntime(4427):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 16:47:36.714: E/AndroidRuntime(4427):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 16:47:36.714: E/AndroidRuntime(4427):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 16:47:36.714: E/AndroidRuntime(4427):     at dalvik.system.NativeStart.main(Native Method)
05-07 16:47:36.724: W/ActivityManager(436):   Force finishing activity com.LudwigAppDesign.streamingradioplayerpro/.Songs
05-07 16:47:36.784: D/dalvikvm(436): GC_FOR_ALLOC freed 3517K, 29% free 17724K/24840K, paused 57ms, total 57ms
05-07 16:47:37.294: W/ActivityManager(436): Activity pause timeout for ActivityRecord{42c1cd18 u0 com.LudwigAppDesign.streamingradioplayerpro/.Songs}
05-07 16:47:46.794: W/ActivityManager(436): Launch timeout has expired, giving up wake lock!
05-07 16:47:47.304: W/ActivityManager(436): Activity idle timeout for ActivityRecord{42b7ced0 u0 com.LudwigAppDesign.streamingradioplayerpro/.Main}
05-07 16:48:05.824: W/ThrottleService(436): unable to find stats for iface rmnet0

I left this out of the playSong method...is this causing the NullPoingterException?

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {

            MusicService.mPlayer
                    .setOnCompletionListener(new OnCompletionListener() {
                        public void onCompletion(MediaPlayer arg0) {
                            nextSong();
                        }
                    });
        }
    }, 1000);

}

private void nextSong() {
    if (++currentPosition >= songs.size()) {    
        currentPosition = 0;
    } else {
        // Play next song
        playSong(MEDIA_PATH + songs.get(currentPosition));
        textViewRecordPlaying.setText(songs.get(currentPosition));
        SharedPreferences settings = getSharedPreferences(
                RECORDINGS_NOW_PLAYING, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("nowPlayingTitle", (songs.get(currentPosition).toString().trim()));
        // Commit the edits!
        editor.commit();
    }
}

EDIT---Hi I removed this code and the NullPointerException is gone:

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {

            MusicService.mPlayer
                    .setOnCompletionListener(new OnCompletionListener() {
                        public void onCompletion(MediaPlayer arg0) {
                            nextSong();
                        }
                    });
        }
    }, 1000);

Now I am just left with the original error of Permission Denied:

05-08 12:23:26.838: I/Streaming Radio Recorder Pro(2661): Playing from URL/path: file:///storage/emulated/0/AudioStreamRecorder/Goudron%20-%20Art%20School%20Fuckheads-5.55.51%20Tuesday%2C%20May%207%2C%202013.mp3
05-08 12:23:26.838: I/AudioService(482):  AudioFocus  requestAudioFocus() from android.media.AudioManager@41cfdfd0com.LudwigAppDesign.streamingradioplayerpro.AudioFocusHelper@41cfdaf8
05-08 12:23:26.848: I/AwesomePlayer(128): setDataSource_l(URL suppressed)
05-08 12:23:26.848: I/MusicRetriever(2661): Querying media...
05-08 12:23:26.858: I/MusicRetriever(2661): URI: content://media/external/audio/media
05-08 12:23:26.858: E/(128): Failed to open file '/storage/emulated/0/AudioStreamRecorder/Goudron - Art School Fuckheads-5.55.51 Tuesday, May 7, 2013.mp3'. (Permission denied)
05-08 12:23:26.858: E/MediaPlayer(2661): error (1, -2147483648)
05-08 12:23:26.858: E/MediaPlayer(2661): Error (1,-2147483648)
05-08 12:23:26.898: D/dalvikvm(584): GC_CONCURRENT freed 518K, 7% free 9075K/9736K, paused 3ms+3ms, total 32ms
05-08 12:23:26.898: E/Streaming Radio Recorder Pro(2661): Error: what=1, extra=-2147483648
05-08 12:23:26.928: I/AudioService(482):  AudioFocus  abandonAudioFocus() from android.media.AudioManager@41cfdfd0com.LudwigAppDesign.streamingradioplayerpro.AudioFocusHelper@41cfdaf8
05-08 12:23:26.928: I/MusicRetriever(2661): Query finished. Returned a cursor.
05-08 12:23:26.928: I/MusicRetriever(2661): Listing...
05-08 12:23:26.928: I/MusicRetriever(2661): Title column index: 8
05-08 12:23:26.928: I/MusicRetriever(2661): ID column index: 8
05-08 12:23:26.928: I/MusicRetriever(2661): ID: 21 Title: Join Hangout
05-08 12:23:26.928: I/MusicRetriever(2661): ID: 1553 Title: Goudron - Art School Fuckheads-5.55.51 Tuesday, May 7, 2013
05-08 12:23:26.928: I/MusicRetriever(2661): ID: 1694 Title: Lusine Icl - Mod-10.47.59 Tuesday, May 7, 2013
05-08 12:23:26.928: I/MusicRetriever(2661): Done querying media. MusicRetriever is ready.

I hope this helps...let me know if you need more info and thanks for trying.

midiwriter
  • 426
  • 5
  • 12
  • Try putting a dummy txt file on the external storage and see if you are able to read it from code. Maybe also try passing the file path with your intent and getting the uri on the service side. I don't see why that would change anything, but no harm in trying. – FoamyGuy May 07 '13 at 23:42
  • Never construct a file path using string concatenation. Please use the proper `File` constructor. Beyond that, please post the entire stack trace. – CommonsWare May 07 '13 at 23:43
  • How do I do the stack trace properly here? – midiwriter May 07 '13 at 23:49

2 Answers2

1

Nexus 7 Failed to open file. Permission denied

That is not what your stack trace indicates. Your error in the stack trace is:

05-07 16:47:36.714: E/AndroidRuntime(4427): java.lang.NullPointerException
05-07 16:47:36.714: E/AndroidRuntime(4427):     at com.LudwigAppDesign.streamingradioplayerpro.Songs$8.run(Songs.java:407)

Since we do not have a run() method in the source code that you have listed above, it is difficult to assist you further.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have the `playSong` method in the list item click. Am I missing something else? Also, isn't the NullPointerException a result of the Permission denied since it's still trying to run? – midiwriter May 08 '13 at 00:02
  • @MikeLudwig: "Am I missing something else?" -- the `NullPointerException` is coming from a `run()` method on some anonymous inner class. "Also, isn't the NullPointerException a result of the Permission denied since it's still trying to run?" -- we have no way of answering that, particularly since we have no idea what this `run()` method is trying to do. – CommonsWare May 08 '13 at 00:06
  • @MikeLudwig: You appear to have not initialized `mPlayer`. That's the only thing I can see in that `run()` that could cause a `NullPointerException`. – CommonsWare May 08 '13 at 00:13
  • Just wondering why it's been working on some devices and not tablets. – midiwriter May 08 '13 at 00:15
  • I'm still looking for a solution to this...looks like a lot of unanswered questions regarding Android 4.2 and not being able to open files on internal storage... – midiwriter May 08 '13 at 05:30
  • @MikeLudwig: "looks like a lot of unanswered questions regarding Android 4.2 and not being able to open files on internal storage" -- you're not working with internal storage. You are working with external storage. I have no idea what the "lot of unanswered questions" are, because you have not linked to them. Beyond that, I cannot help you determine why `mPlayer` is not initialized, because from the few code fragments shown here, you never even try to initialize it, suggesting that this code is not in your question. – CommonsWare May 08 '13 at 10:43
  • Hi I added more edits and the `NullPointerException` is gone...thanks. – midiwriter May 08 '13 at 19:33
  • The file is trying to be opened from `/storage/emulated/0/AudioStreamRecorder`. When I use ES File Explorer the path is `/sdcard/AudioStreamRecorder`. I have a feeling this is where the error is coming from but have no idea how to fix it...I'm using `new String(Environment.getExternalStorageDirectory().getPath() + "/AudioStreamRecorder/"` to get my path. Is this right on 4.2.2? – midiwriter May 08 '13 at 19:40
  • @MikeLudwig: Well, you're using concatenation, as I pointed out before as not being ideal. `/sdcard` is a symlink pointing elsewhere, and `/storage/emulated/0` is certainly a possible destination. Beyond that, the only thing I can think of is for you to try a filename without spaces. – CommonsWare May 08 '13 at 20:11
  • No that didn't work removing the spaces. The file is there because I have a `TextView` that reads the file name/location and it is correct and the file plays in other programs. Something about 4.2 doesn't like the way I am trying to access the file and I just need to figure out what it is (even works on 4.1 and below). – midiwriter May 08 '13 at 21:45
  • @MikeLudwig: If you can create a reproducible test case -- a project, plus instructions on how to use that project to reproduce your error -- I'd like to take a look at it. – CommonsWare May 09 '13 at 12:12
  • OK I'll do it later when I have time. Thanks. – midiwriter May 09 '13 at 14:36
  • In the meantime here's a post I'm looking at: [link]http://stackoverflow.com/questions/13661877/access-sdcard-in-android-4-2 – midiwriter May 09 '13 at 14:53
  • @MikeLudwig: Well, the multi-user capability of Android 4.2+ means that you can only get at the current user's external storage. A path to User A's external storage will be inaccessible from User B's account. But you'd have to go a bit out of your way to get A's path into your app when running as B. But that's why a reproducible test case is important, so we can determine if the problem is in Android or in the behavior of the test code itself. – CommonsWare May 09 '13 at 16:12
  • thank you very much. I'm very bogged down with work the next two days but I will try to get that to you ASAP. Thanks again – midiwriter May 09 '13 at 16:20
0

4.2 added profiles, and it seems the MediaPlayer never got updated to correctly handle this.

get a FileDescriptor for your URL and play that instead and the problem should go away. Noticed this after 2 weeks of developing on my SGSII and then switching to my Nexus 7.

setDataSource(new FileInputStream(url).getFD());

Ruxton
  • 656
  • 6
  • 14
  • Hi thank you for the reply. If you could please help, how would I do this using an intent? Here's my code: `final Intent i = new Intent(MusicService.ACTION_URL); File file = new File(songPath); Uri uri = Uri.fromFile(file); i.setData(uri); startService(i);` This must be where the problem is since I can play the same file if I use `MediaPlayer` directly without the `Intent`. – midiwriter May 18 '13 at 15:08
  • something inside MusicService is causing your problem, how are you setting the uri inside MusicService? – Ruxton May 30 '13 at 12:00