如何在 j2me 中下载和播放视频?
我的代码可供下载
在三星中:下载完成为以相同字节写入但不支持文件格式的相同字节文件,
在nokia2690下载未完成,显示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();
}
}
}