J2ME Networking:Interacting Server PHP(Image Fetch)

You can even retrieve binary data such as images. If the mobile device is capable of view documents like word or PDF then you should be able to send those by HTTP as well.

J2ME Source Code:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class SimpleImageFetch extends MIDlet
{
private Display display;
private String URL = "http://127.0.0.1/midlet/image/test.php";
private Form formImage;

public SimpleImageFetch()
{
try {
display = Display.getDisplay(this);
Image im = getImage(URL);
formImage = new Form("Simple Image Test");
formImage.append(im);
display.setCurrent(formImage);
} catch (Exception ex) {
System.out.println(ex);
}

}

public void startApp() { }
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }

private Image getImage(String url) throws IOException
{
ContentConnection connection = (ContentConnection) Connector.open(url);
DataInputStream iStrm = connection.openDataInputStream();
Image im = null;

try {
byte imageData[];
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);
imageData = bStrm.toByteArray();
bStrm.close();
im = Image.createImage(imageData, 0, imageData.length);
} finally {
if (iStrm != null)
iStrm.close();
if (connection != null)
connection.close();
}
return (im == null ? null : im);
}
}

PHP Source Code:

$filename = "./phpjava.png";
$handle = fopen ($filename, "rb");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);
header("Content-type: image/gif");
header("Content-length: ".(string)(filesize($filename)));
echo $contents
?>

2 comments:

  1. hey,this article was really good! Exactly how can you upload an image from your J2ME app to a server? Please help.

    ReplyDelete
  2. hey,how can you upload an image from J2ME app to a server. I am having did different fields to uploade I have to send text and image ? Please help.

    ReplyDelete

 

Design by Blogger Buster | Distributed by Blogging Tips