【发布时间】:2019-03-06 04:06:39
【问题描述】:
我是 java 和 javascript 编程的新手。目前我正在做一个应用程序来获取手机存储和 sd 卡中的所有 mp3 并将其传递给 @javascriptinterface 然后传递给 html 文件中的 javascript。当结果显示在 html 时,它会显示包文件名。例如com.xxx.xxx.xxx@12345.
需要在 html 中获取 mp3 名称显示。
任何建议将不胜感激。谢谢。
这是我的活动代码:
public ArrayList<AudioModel> getAllAudioFromDevice (final Context context){
final ArrayList<AudioModel> tempAudioList = new ArrayList<>();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; // retrieve song info
String[] projection = {
MediaStore.Audio.AudioColumns.DATA,
MediaStore.Audio.AudioColumns.TITLE,
MediaStore.Audio.AudioColumns.ALBUM,
MediaStore.Audio.ArtistColumns.ARTIST,
MediaStore.Audio.AudioColumns.DURATION,
};
Cursor c = context.getContentResolver().query(uri, // Find from the database according to Uri
projection, null, null, null);
if (c != null) {
while (c.moveToNext()) {
AudioModel audioModel = new AudioModel();
String path = c.getString(0); // Retrieve path.
String name = c.getString(1); // Retrieve name.
String album = c.getString(2); // Retrieve album name.
String artist = c.getString(3); // Retrieve artist name.
String duration = c.getString(4);
audioModel.setaName(name);
audioModel.setaAlbum(album);
audioModel.setaArtist(artist);
audioModel.setaPath(path);
audioModel.setaDuration(duration);
Log.e("Name :" + name, " Album :" + duration);
Log.e("Path :" + path, " Artist :" + artist);
tempAudioList.add(audioModel);
}
c.close();
}
return tempAudioList; // stop getAllAudioFromDevice method
}
public class JavaScriptInterface {
private Activity activity;
Context mContext;
public void JavaScriptInterface(Context c) {
mContext = c;
}
public JavaScriptInterface(Activity activity) {
this.activity = activity;
}
@JavascriptInterface
public String getmp3all() {
getAllAudioFromDevice(MyActivity.this).toString();
}
}
下面是html文件:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<tr>
<td>
<div id="divMp3"></div>
</td>
</tr>
</table>
<script type="text/javascript">
function showMP3(){
var arraymp3 = AndroidFunction.getmp3all();
document.getElementById('divMp3').innerHTML = arraymp3;
}
window.onLoad = showMP3();
</script>
</body>
</html>
更新: 设法解决上述问题。
@JavascriptInterface
public String getmp3all() {
ArrayList<AudioModel> allMp3 = getAllAudioFromDevice(this.activity);
List<AudioModel> listStrings = new ArrayList<>(allMp3);
String temp= "";
for (int i = 0; i < listStrings.size(); i++) {
temp += listStrings.get(i).aName+",";
}
return temp;
}
HTML 文件:
<table>
<tbody>
<tr>
<script>
function showMP3(){
var stringmp3 = AndroidFunction.getmp3all();
var arraymp3= stringmp3.split(",");
for(var i = 0; i < arraymp3.length; i++)
{
document.write("<tr>");
document.write("<td>"+ arraymp3[i] +"</td>");
document.write("</tr>");
console.log(arraymp3[i]);
}
}
window.onLoad = showMP3();
</script>
</tr>
</tbody>
</table>
【问题讨论】:
-
嗨 Josephcvh,欢迎来到 Stack Overflow!请你能更清楚一点问题是什么?
-
嗨 user230910,当我运行这个应用程序时出现问题,结果显示“com.test.admin.testinghtml.AudioModel@12345678”而不是 mp3 文件详细信息。
标签: javascript java android html