【问题标题】:Android raw resource file not readableAndroid原始资源文件不可读
【发布时间】:2013-10-09 03:24:47
【问题描述】:

我有几个 mp3 文件作为原始资源添加到我的项目中。我想用 AudioPlayer 播放它们。我有 FileNotfoundException 并且我回溯它以发现可以找到文件但它们不可读: mp1.stop(); mp1.reset();

    playedSound = R.raw.sip02;
    String str = getResources().getString(playedSound);
    File file = new File(str);
    //((File)file).setReadable(true);
    boolean readable = file.canRead();      //returns TRUE
    file.setReadOnly();
    readable = file.canRead();              //returns TRUE
    FileInputStream inputStream = null;
                try {
        inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();                 //enters this branch
        }

我无法调用 file.setReadable(),因为我使用的是 API 级别 7。

我的清单:

   <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="com.example.movingcircle"
       android:versionCode="1"
       android:versionName="1.0" >

       <uses-sdk
           android:minSdkVersion="8"
           android:targetSdkVersion="8" />

       <application
           android:allowBackup="true"
           android:icon="@drawable/ic_launcher"
           android:label="@string/app_name"
           android:theme="@style/AppTheme" >
           <activity
               android:name="com.example.movingcircle.MovingCircle"
               android:label="@string/app_name" >
               <intent-filter>
                   <action android:name="android.intent.action.MAIN" />

                   <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
           </activity>
       </application>

   </manifest>

【问题讨论】:

  • 你想播放mp3??
  • 我认为问题不在于播放 mp3。我可以在另一个应用程序中使用此代码播放 mp3。在这种特殊情况下,我发现 mp3 不可读。
  • getResources().getString(playedSound); 从 strings.xml 返回一个字符串,而不是从 raw 文件的路径
  • 不,它没有。它返回“res/raw/sip02.mp3”

标签: android file-permissions android-2.2-froyo


【解决方案1】:

试试

  MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.your_mp3);

【讨论】:

猜你喜欢
  • 2011-05-04
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多