Nexus 7无法打开文件。没有权限

时间:2013-05-07 23:39:17

标签: android android-listview nexus-7

刚拿到我的Nexus 7,当我播放音频文件时,我收到此错误,然后关闭一个力量。我一整天都在寻找解决方案。这是相关的代码:

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

我的MEDIA_PATH

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

这段代码在我的Galaxy S3和我的Thunderbolt上运行得很漂亮,但是当我点击列表项时,我在Nexus 7和Asus Transformer Prime TF201上出现Failed to open file. Permission denied.错误,然后一个力关闭。

我尝试过Environment.getExternalStorageDirectory();Environment.getExternalStorageDirectory()getAbsolutePath();但错误相同。我已宣布<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />和     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 有人请帮助我用完了想法......

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

我把它从playSong方法中删除了...这会导致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();
    }
}

编辑---嗨我删除了这段代码,NullPointerException消失了:

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

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

现在我只剩下拒绝权限的原始错误:

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.

我希望这会有所帮助......如果您需要更多信息并感谢您尝试,请告诉我。

2 个答案:

答案 0 :(得分:1)

  

Nexus 7无法打开文件。许可被拒绝

这不是您的堆栈跟踪指示的内容。堆栈跟踪中的错误是:

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)

由于我们在上面列出的源代码中没有run()方法,因此很难为您提供进一步的帮助。

答案 1 :(得分:0)

4.2添加了配置文件,似乎MediaPlayer从未更新以正确处理此问题。

为您的网址获取一个FileDescriptor并改为播放,问题应该消失。在我的SGSII开发2周后再切换到我的Nexus 7时注意到这一点。

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