Reputation: 862
I am working on a project, in which my android is working as a web server; enter the IP address with a port number and a web interface is open from and user can upload Files to mobile. i want to show some pictures on web interface so that we interface looks good.
How to give reference of images in draw-able or Raw or assets folder in image src"" fiels
here is my code:
private String getZipLink(long folderId) {
return "<a href=\"/zip/" + folderId + "/folder.zip\"><img src=\"file:///android_res/raw/img\" />" +
"Zip of Entire Folder</a>";
How to add image there ? }
Upvotes: 1
Views: 177
Reputation: 371
You can do something like this
/**
* This method starts the web server listening to the port 8080
*/
protected void start() {
new Thread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "Secure Web Server is starting up on port 8080");
try {
// Create the secure server socket
sss = (SSLServerSocket) sssf.createServerSocket(8080);
} catch (Exception e) {
System.out.println("Error: " + e);
return;
}
Log.d(TAG, "Waiting for connection");
while (isRunning) {
try {
// Wait for an SSL connection
Socket socket = sss.accept();
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("lsof -i:"+socket.getPort());
System.out.println("Process:::"+pr.getInputStream());
InputStream in1 = pr.getInputStream();
InputStreamReader is = new InputStreamReader(in1);
StringBuilder sb=new StringBuilder();
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
while(read != null) {
//System.out.println(read);
sb.append(read);
read =br.readLine();
}
System.out.println("Process read:::"+sb.toString());
// Got a connection
Log.d(TAG, "Connected, sending data.");
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket
.getOutputStream());
// Read the data until a blank line is reached which
// signifies the end of the client HTTP headers
String str = ".";
while (!str.equals(""))
str = in.readLine();
// Send a HTTP response
out.println("HTTP/1.0 200 OK");
out.println("Content-Type: text/html");
out.println("Server: Android KeyChainiDemo SSL Server");
// this blank line signals the end of the headers
out.println("");
// Send the HTML page
out.println("<H1>Welcome to Android!</H1>");
// Add an embedded Android image
out.println("<img src='data:image/png;base64," + base64Image + "'/>");
out.flush();
socket.close();
} catch (Exception e) {
Log.d(TAG, "Error: " + e);
}
}
}
}).start();
}
base64Image = createBase64Image(ctx);
/**
* This method reads a binary image from the assets folder and returns the
* base64 encoded image string.
*
* @param ctx The service this web server is running in.
* @return String The base64 encoded image string or "" if there is an
* exception
*/
private String createBase64Image(Context ctx) {
BufferedInputStream bis;
try {
bis = new BufferedInputStream(ctx.getAssets().open(**image file stored in assets folder **));
byte[] embeddedImage = new byte[bis.available()];
bis.read(embeddedImage);
return Base64.encodeToString(embeddedImage, Base64.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
So place your image in assets folder of android
For more clarification see the KeyChainDemo in android APIDemos in Android samples for Android4.0
Upvotes: 0
Reputation: 36654
if you have control over the request handling in your web server, you can use a image URL like
<img src="images/myimage.png" ... />
and handle the request to the resource images/myimage.png
by returning a byte stream, with the correct MIME type image/png
. This is very similar to the code used in Java EE Servlets, so you can search for example code and adapt it to your environment.
Upvotes: 0
Reputation: 56123
Perhaps you can embed the image data in your HTML (instead of trying to provide a link to the image data): see Inline Images with Data URLs.
Upvotes: 1