Note that there are some explanatory texts on larger screens.

plurals
  1. POPhoneGap 3.1 Android copy audio file from www folder to SD card
    primarykey
    data
    text
    <p>Here is the problem. I got an app that download audio files to the phone then play it back. All works without problem. But the little problem is - the customer wants to have something that play straight away when the other files are being download. </p> <p>So I put an short audio in the www/files/ folder. That works without any issue with iOS. I just reference it (from media player) as </p> <pre><code>document.addEventListener('deviceready' , function() { var media = new Media('files/default.m4a' , function() { // this success call back never works! }); setTimeout(function() { media.play(); },100); }); </code></pre> <p>Now the problem is Android. Got a FileError.NOT_FOUND_ERR error. Looking at LogCat (From Android studio)</p> <p>The path become </p> <pre><code> /mnt/sdcard/files/default.m4a </code></pre> <p>It seems that Android couldn't handle a relative path like iOS (strange enough all the images or template are relative path in the js app. And they all work fine). </p> <p>Been searching up and down a solution how do I copy just that one audio file to the sd card. no luck so far. The problem is I couldn't correctly find the absolute path to the original file. There are lots of example out there which is total BS. They hardcoded the path in! Android can be anything, in the emulator is /mnt/sdcard/ on my test device turns into /external/sdcard0 </p> <p>And that just from (java):</p> <pre><code> String path = Environment.getExternalStorageDirectory().getAbsolutePath(); </code></pre> <p>I seen tons and tons of people got the same problem. But the solution out there are pretty out date. Or not really solve the first problem (which is finding out where is the original file in the first place). There must be one problem many people face right? </p> <p>Thanks in advance. </p> <p>P.S. I also try this</p> <pre><code> this.getClass().getPackage().getName(); </code></pre> <p>call from inside the onCreate method. That only got me partial but not the absolute path. </p> <p><em>UPDATE</em></p> <p>Here is the complete working code (for PhoneGap 3.1)</p> <pre><code>package YOUR_PACKAGE_NAME; import android.os.Bundle; import org.apache.cordova.*; /* for the other two plugins */ import java.io.*; import android.os.Environment; import android.util.Log; import android.content.res.AssetManager; public class YourAppName extends CordovaActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.init(); this.CopyAssets(); super.loadUrl(Config.getStartUrl()); } private void CopyAssets() { AssetManager assetManager = getAssets(); InputStream in = null; OutputStream out = null; try { in = assetManager.open(FILE_YOU_WANT_TO_COPY_IN_WWW); out = new FileOutputStream(Environment.getExternalStorageDirectory() .toString() + WHERE_YOU_WANT_TO_PUT_IT_PLUS_NAME); this.copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch (Exception e) { Log.e("tag", e.getMessage()); } } private void copyFile(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload