Search code examples
javaandroidcordovaphonegap-pluginsabsolute-path

Cordova/PhoneGap Get File Path To Resource (Outside of Res Folder)


We have a Cordova/PhoneGap project and we are trying to play an audio file.

We have tried dozens of attempts to get the absolute file path and failed.

e.g.

    File file = new File("android.resource://www/audio/0.m4a");
               //also tried "www/audio/0.m4a"
               //also tried "file:///assets/www/audio/0.m4a"
           if(file.exists())
           {
               Log.d("1","FILE EXISTS");
           }else
           {
               Log.d("2","FILE DOES NOT EXIST");
           }

The file exists and is in the folder

enter image description here

And the file will be found (and play) if I move it to the /res/raw folder and use the R.res.0 to play it.

Do you know how I can get the absolute path to this audio file so I can create a File object from it?


Solution

  • Looks like you can't create file object from Assets.

    Few Questions :

    Why you are keeping file in assets folder, when you have res->raw folder ?

    If you want to play from Assets, you need to do this

    final MediaPlayer mediaPlayer=new MediaPlayer();  
    final AssetFileDescriptor assetFileDescritor=pContext.getAssets().openFd(MusicFactory.sAssetBasePath
                        + pAssetPath); mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(),assetFileDescritor.getStartOffset(),assetFileDescritor.getLength()); 
    mediaPlayer.prepare();
    final Music music=new Music(pMusicManager,mediaPlayer);
    pMusicManager.add(music);
    return music;