Android Application to Send a File to Remote Server

Posted by Unknown Jumat, 30 Maret 2012 0 komentar
This simple Android application sends a file to a remote server. In this example code server runs on local host. When the Send button is clicked the file is sent to the server.

Android Client Application
SimpleClientActivity.java
/*
* This is a simple Android mobile client
* This application send any file to a remort server when the
* send button is pressed
* Author by Lak J Comspace
*/

package lakj.comspace.simpleclient;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SimpleClientActivity extends Activity {

private Socket client;
private FileInputStream fileInputStream;
private BufferedInputStream bufferedInputStream;
private OutputStream outputStream;
private Button button;
private TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.button1); //reference to the send button
text = (TextView) findViewById(R.id.textView1); //reference to the text view

//Button press event listener
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {


File file = new File("/mnt/sdcard/input.jpg"); //create file instance

try {

client = new Socket("10.0.2.2", 4444);

byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file

fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);

bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file

outputStream = client.getOutputStream();

outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();

text.setText("File Sent");


} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
});

}
}


main.xml






AndroidManifest.xml


















Server Application
main.java
/*
* This is a simple server application
* This server receive a file from a Android client and save it in a given place.
* Author by Lak J Comspace
*/
package simpleserver;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStream inputStream;
private static FileOutputStream fileOutputStream;
private static BufferedOutputStream bufferedOutputStream;
private static int filesize = 10000000; // filesize temporary hardcoded
private static int bytesRead;
private static int current = 0;

public static void main(String[] args) throws IOException {


serverSocket = new ServerSocket(4444); //Server socket

System.out.println("Server started. Listening to the port 4444");


clientSocket = serverSocket.accept();


byte[] mybytearray = new byte[filesize]; //create byte array to buffer the file

inputStream = clientSocket.getInputStream();
fileOutputStream = new FileOutputStream("D:\\output.jpg");
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

System.out.println("Receiving...");

//following lines read the input slide file byte by byte
bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
current = bytesRead;

do {
bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);


bufferedOutputStream.write(mybytearray, 0, current);
bufferedOutputStream.flush();
bufferedOutputStream.close();
inputStream.close();
clientSocket.close();
serverSocket.close();

System.out.println("Sever recieved the file");

}
}


Baca Selengkapnya ....

Simple Client-Server Application for Android

Posted by Unknown Kamis, 29 Maret 2012 0 komentar
This application is a simple client-server application which has a Android mobile client and a Java server which is run on a machine. In this example, client is run on the Android emulator and the server is run on the local host. In Android 10.0.2.2 is the IP address for local host. This application allow to type a text message on a text field and when the Send button is press the message is sent to the server. Server continuously listen to the port. When there is a incoming message server read it and show it on the standard output.



Client Side Application


SlimpleClientActivity.java

/*
* This is a simple Android mobile client
* This application read any string messege typed on the text field and
* send it to the server when the Send button is pressed
* Author by Lak J Comspace
*/

package lakj.comspace.simpleclient;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SimpleClientActivity extends Activity {

private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button;
private String messsage;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textField = (EditText) findViewById(R.id.editText1); //reference to the text field
button = (Button) findViewById(R.id.button1); //reference to the send button

//Button press event listener
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

messsage = textField.getText().toString(); //get the text message on the text field
textField.setText(""); //Reset the text field to blank

try {

client = new Socket("10.0.2.2", 4444); //connect to server
printwriter = new PrintWriter(client.getOutputStream(),true);
printwriter.write(messsage); //write the message to output stream

printwriter.flush();
printwriter.close();
client.close(); //closing the connection

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});

}
}


main.xml












AndroidManifest.xml



















Server Side Application


main.java

/*
* This is a simple server application
* This server receive a string message from the Android mobile phone
* and show it on the console.
* Author by Lak J Comspace
*/
package simpleserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;

public static void main(String[] args) {

try {
serverSocket = new ServerSocket(4444); //Server socket

} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}

System.out.println("Server started. Listening to the port 4444");

while (true) {
try {

clientSocket = serverSocket.accept(); //accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
message = bufferedReader.readLine();

System.out.println(message);
inputStreamReader.close();
clientSocket.close();

} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}

}
}

Baca Selengkapnya ....

You choose my next review

Posted by Unknown Rabu, 28 Maret 2012 0 komentar
Hi guys, I have decided to let you guys, my loyal followers, decide on which app/game I review next. I've set up a small poll on the right of this site, please vote what you'd like me to review next...

Thanks!

Baca Selengkapnya ....

100k and counting

Posted by Unknown Selasa, 20 Maret 2012 0 komentar
Today this blog of mine passed the 100000 page view mark. Thanks for everyone who has visited so far! I'll keep the posts coming as long as there's interest!

I'm quite active on Twitter as well, so if you want, you can follow me on twitter as well. And for those interested, I have some other blogs going also:

  • Battlefield 3 Platoon blog - I like Battlefield 3 for PC, and have some nice tips and tricks regarding the game here
  • My Herbalife blog - I've lost 16kg since starting on the amazing Herbalife products. I am now blogging about my experiences
  • Sugar + Spice blog - this is my wife's blog - she likes all pretty things :)
Anyway, Thanks again for the great support!

Cheers,
Rudi

Baca Selengkapnya ....

Humble Bundle for Android 2!!!

Posted by Unknown 0 komentar
Hi guys, I have excellent news! The awesome guys from Humble Bundle are running another Android bundle!!! For those of you who still don't know what the Humble Indie Bundle is, it's basically a bunch of independent developers who makes their games available through the Humble Bundle site, and then, the best part is that YOU can decide how much to pay for them!

You can even then allocate the money you're spending as you want to the developers, a charity or the guys from Humble Bundle.

This time around, they have the following games available on the bundle:

  • Canabalt
  • Zen Bound 2
  • Cogs
  • Avadon: The Black Fortress
And if you pay more than the average people are paying, you get Swords & Soldiers included in your bundle as well! And, based on past experiences, they usually add another game or two as the bundle comes to an end... ;)

Edit: They've added another game!! (I like it when they do this!) - Snuggle Truck is now part of the bundle :)

Now, Avadon appears to only be for tablets, but the rest I've already downloaded and tried, and they are pretty awesome! I'm really liking Zen Bound 2 at the moment, here's a couple of screen shots of it in action:



I'll do a review of it soon, it's really a fun little puzzle game :)

They've already sold 53000+ copies as I'm writing this post, and there's still 13 days remaining! So head on over to www.humblebundle.com and get yours as well!

Enjoy!

Baca Selengkapnya ....

All about you :)

Posted by Unknown Senin, 19 Maret 2012 0 komentar
This post will be all about you guys - I started this blog in August last year, and didn't have any sort of clue that it will draw as many visitors as it does!

So firstly, thanks to everyone who visits, I really appreciate the time you take to read what I have to say.

Secondly, I have decided to get some feedback from you guys, so, by leaving a comment, please let me know the following:
  1. Your favourite GAME
  2. Your favourite APP (has to be NOT a game hehe...)
I'm always on the lookout for decent apps and games for my Galaxy S2, so please, share with us! I'll use the comments and compile it into its own post as soon as I have enough feedback.

Thanks in advance!

Baca Selengkapnya ....

App review: Pixlr-o-matic Photo editor

Posted by Unknown Kamis, 15 Maret 2012 0 komentar
The other day I was browsing the app market, which they've renamed to the "Play Store", and I saw this little gem of an app called Pixlr-o-matic, and best of all it's a free one!

Usually I don't have high hopes for free apps like this, but I thought I'd give it a go anyway.

What it basically does is it edits your photos, by doing a combination of three effects, namely a colouring effect, lighting effect and a frame effect.

Then you can save and share your photos with ease from within the app itself. Anyway, when you open the app, here's what you'll see first:


From here you select which photo to start working on. This can either come from your phone's camera, be one of your existing photos, or as you can see, they have some of their own photos there that you can use to play around with.

Once you've selected the photo you want to work on, you get to this screen:


Here you'll be presented with several buttons and things. In the middle you'll see the photo I selected to edit, which as you can see is a bit of a boring one...

At the top there's a little film roll Icon - if you tap on that, you can get access to a lot more presets and options, all for free - I went crazy, and downloaded everything :)


Next to the film roll icon is the randomize button. That does just that, it randomizes the 3 different effects you can get. So if you're lazy, you can just hit that one a couple of times until you get to a combination you like. Here I quickly set a preset for each of the three options:

On the first preset, I selected "Dean" which gives the photo a washed out look, almost like a photo taken in the 60's :)

Here on the Lighting option I selected "Gaze", which gives it this red streak in the middle, giving it an even more classic and washed out look.


And here I added the "Dirt" frame, which as you can see, puts a dark edge with dust specks and stuff like that around the image.

So in a matter of seconds, I transformed a pretty dull photo into something a bit more lively :) Obviously you can save your photo as well - the app gives each image a unique file, so you don't have to worry about it overwriting the original.


From here you can Save your photo and share it.

I'm very impressed with how quick and easy it is to use this app, and most of the effects are really cool (of course there's some corny ones as well, but sometimes that's actually the look you're going for!)

As a little test and just to show you, I took two images and made 5 randomized versions. Here they are, the one at the top is the original:


Pretty neat huh? And here's a photo of Einstein (our mutt :)


Not too shabby for just hitting a button a few times.

If you like photos at all, then this is definitely a must to get.

Get it here.

Till next time!

Baca Selengkapnya ....

Galaxy S2 Ice Cream Sandwich Update

Posted by Unknown Selasa, 13 Maret 2012 0 komentar
Hi all, everyone is waiting in great anticipation for our beloved Galaxy S2 to finally get updated to Ice Cream Sandwich, which is version 4.0.3 of the Android Operating system.

Edit: On 7 April, here's how the ICS updates are coming along:
  • Australia
  • Austria
  • Belgium
  • China
  • Cyprus CYO, CYV
  • Denmark
  • Hong Kong TGY
  • Hungary XEH (unlocked)
  • Indonesia
  • Ireland (Three)
  • Italy is busy updating (means they're testing, will be soon now!)
  • Korea
  • Lithuania
  • Luxemberg
  • Netherlands also busy updating
  • Philippines (XTC, GLB)
  • Poland XEO
  • Russia SER
  • Sweden (Tre)
  • Taiwan
  • UK (Orange, O2, T-Mobile and Vodafone) busy updating
  • UK (unlocked) busy updating
  • UK (Three)
  • Nordic Countries NEE (Denmark, Finland, Norway, Sweden, Iceland, Greenland)
    I just checked here as well, and we South Africans are definitely not on the list either :(

    Edit: Thanks Philipp, he phoned Samsung South Africa, and it seems we're only getting ICS the end of April here in SA. That sucks!

    If you live in the one of the above countries, and you have updated your phone, please leave some comments and tell us your experiences so far :)

    Will keep everyone posted!

    Baca Selengkapnya ....

    Full Screen Status Bar

    Posted by Unknown 0 komentar
    If you regularly use apps that go full screen on your Android phone, you would have noticed that you cannot get access to the status bar (you know, the thing you swipe down from the top to get?) - but, I've found a very handy little app that allows you to get to your status bar while in a full screen app (think GAMES :)

    Here's a screen shot of it in action:


    See, here in the Freeplay Sims game, at the top you'll see my status bar peeking out. A very handy little app.

    Anyway, it's called Smart Statusbar, and you can get it here

    Till next time!

    Baca Selengkapnya ....
    Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of android photography.