如何在J2ME中下载和播放视频

时间:2013-02-14 13:04:47

标签: java-me download midp mmapi jsr75

如何在j2me下载和播放视频?

我的代码正在下载

在三星中:下载完成,因为相同的字节文件以相同的字节写入,但不支持文件格式,

在诺基亚2690下载未完成时,它显示NullpointerException

我在

下添加了我的代码
  private void DownloadStreams(String url, String filepath) throws IOException {

    InputStream is = null;
    StringBuffer sb = new StringBuffer();
    HttpConnection http = null;
    try {
        Alert all = new Alert("url stream::");
        all.setString("download stream!!:");
        all.setTimeout(1000);
        display.setCurrent(all);
        URL = EncodeURL(videoURL);
        http = (HttpConnection) Connector.open(videoURL);
        http.setRequestMethod(HttpConnection.GET);
        if (http.getResponseCode() == HttpConnection.HTTP_OK) {
            DataInputStream dataIn = http.openDataInputStream();
            byte[] buffer = new byte[(int) http.getLength()];
            int read = -1;
            dataIn.readFully(buffer);
            dataIn.close();
            http.close();
            writeFile(buffer, filepath);
            Alert al = new Alert("RESPONSE::" + http.getResponseCode());
            al.setString("download complete!!:");
            al.setTimeout(3000);
            display.setCurrent(al, midp.diS);
        }


    } catch (Exception ex) {
        Alert al = new Alert("Downloading Failed:::" + ex.toString());
        al.setString("Download Failed" + ex.getMessage());
        al.setTimeout(Alert.FOREVER);
        display.setCurrent(al, midp.diS);
    }
}

public void writeFile(byte[] videoBuffer, String filepath) {
    Connection con = null;
    OutputStream os = null;
    try {
        con = Connector.open(filepath, Connector.READ_WRITE);
        FileConnection fc = (FileConnection) con;
        if (!fc.exists()) {
            fc.create();
        }
        os = fc.openOutputStream();
        os.write(videoBuffer);
        os.flush();
        Alert al = new Alert("file write Complete!!" + videoBuffer.length);
        al.setString("write complete!!:" + videoBuffer.length);
        al.setTimeout(3000);
        display.setCurrent(al, midp.diS);
    } catch (Exception e) {
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

0 个答案:

没有答案