[英]PhoneGap 3.1 Android copy audio file from www folder to SD card
這是問題所在。 我有一個應用程序,可以將音頻文件下載到手機上,然后播放。 一切正常。 但是,小問題是-客戶希望在下載其他文件時立即播放某些內容。
因此,我在www / files /文件夾中放了一段簡短的音頻。 這對於iOS沒有任何問題。 我只是引用它(來自媒體播放器)為
document.addEventListener('deviceready' , function()
{
var media = new Media('files/default.m4a' , function()
{
// this success call back never works!
});
setTimeout(function()
{
media.play();
},100);
});
現在的問題是Android。 得到了FileError.NOT_FOUND_ERR錯誤。 查看LogCat(來自Android Studio)
路徑變成
/mnt/sdcard/files/default.m4a
似乎Android無法像iOS那樣處理相對路徑(奇怪的是,所有圖像或模板都是js應用程序中的相對路徑。它們都可以正常工作)。
一直在尋找一種解決方案,如何將一個音頻文件僅復制到SD卡。 到目前為止沒有運氣。 問題是我無法正確找到原始文件的絕對路徑。 有很多例子,這是總的BS。 他們對路徑進行了硬編碼! Android可以是任何東西,模擬器中的/ mnt / sdcard /在我的測試設備上會變成/ external / sdcard0
那只是來自(java):
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
我看到成千上萬的人遇到了同樣的問題。 但是那里的解決方案已經過時了。 還是不能真正解決第一個問題(首先要找出原始文件在哪里)。 許多人必須面對一個問題嗎?
提前致謝。
PS我也嘗試這個
this.getClass().getPackage().getName();
從onCreate方法內部調用。 那只是讓我偏偏,而不是絕對。
UPDATE
這是完整的工作代碼(適用於PhoneGap 3.1)
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);
}
}
}
嘗試這個 :
public class testsample extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
CopyAssets();
super.setIntegerProperty("loadUrlTimeoutValue", 6000);
super.loadUrl(Config.getStartUrl());
}
private void CopyAssets() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("www/img/logo.png"); // if files resides
// inside the "Files"
// directory itself
// "www/img/logo.png" is path of assets/www folder
out = new FileOutputStream(Environment
.getExternalStorageDirectory().toString()
+ "/"
+ "logo.png");
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);
}
}
}
不要忘記在AndroidManifest.xml上添加權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
較干凈的選擇是安裝org.apache.cordova.file-transfer插件,並將文件從“ file:///android_asset/www/song.mp3”傳輸到/YourApp/song.mp3
cordova plugin add org.apache.cordova.file@1.1.0
cordova plugin add org.apache.cordova.file-transfer
接着:
// Assumes variable fileURL contains a valid URL to a path on the device,
// for example, cdvfile://localhost/persistent/path/to/downloads/
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/download.php");
fileTransfer.download(
uri,
fileURL,
function(entry) {
console.log("download complete: " + entry.toURL());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{
//headers: {
// "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
//}
}
);
此處的更多信息: https : //github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md
這樣,您只需要擔心您的javascript
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.