【问题标题】:Android Javascript HTML showing package name as valueAndroid Javascript HTML将包名称显示为值
【发布时间】: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


【解决方案1】:

欢迎使用 stackoverflow。

首先,你需要为你的 webview 设置 javascript 接口。

JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");

接下来,从 javascript 代码中调用该接口。

function showMP3(){
    var arraymp3 = window.JSInterface.getmp3all();
    document.getElementById('divMp3').innerHTML = arraymp3;
}

请参考:Call Java function from JavaScript over Android WebView


更新:

您的getmp3all() 方法不返回任何内容。打印的奇怪数字是内存地址,这意味着您返回的是“类”或“方法”,而不是一个值。

@JavascriptInterface
public String getmp3all() {

    // Is this code compilable? 
    // getAllAudioFromDevice(MyActivity.this).toString();
    return getAllAudioFromDevice(MyActivity.this).toString();
}

【讨论】:

  • 已经包含在我的 onCreate 中:myWebView.getSettings().setJavaScriptEnabled(true); myWebView.addJavascriptInterface(new JavaScriptInterface(this), "AndroidFunction"); myWebView.loadUrl("file:///android_asset/www/index.html");
  • @josephcvh 然后,更改 AndroidFunction.getmp3all();到 window.AndroidFunction.getmp3all();
  • 试过了,结果一样。它显示 com.test.admin.testinghtml.AudioModel@12345678, com.test.admin.testinghtml.AudioModel@23456789, com.test.admin.testinghtml.AudioModel@12312456
  • 我明白了,对不起,请多多包涵,因为我对 android 和 java 还很陌生。也就是说,我需要将 getAllAudioFromDevice 方法内部的值传递给 getmp3all() 方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多