3

I recently started learning Android and I want to make an application that takes a picture from camera intent and sends it to a server, but I can only take a picture. Someone can help me? Now place the code. Client...

public class SendPhoto extends Activity {

private int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    int i;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


    File file = new File(Environment.getExternalStorageDirectory(), "temp/test.jpg");
    Uri outputFileUri = Uri.fromFile(file);


    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    // start the image capture Intent

    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);


 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   int i;
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

                try{
                FileInputStream fis = new FileInputStream("sd/temp/test.jpg");

                Socket sock = new Socket ("hostname",3333);
                DataOutputStream os = new DataOutputStream(sock.getOutputStream());
                while ((i = fis.read()) > -1)
                    os.write(i);

                fis.close();
                os.close();
                sock.close();
                } catch (Exception e) {
                    e.printStackTrace();

                }

        }
    }
}

Server...

public class Server {

/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    ServerSocket socket = new ServerSocket(3333);
    System.out.println("Server started. Listening to the port 3333");
    while (true) {

        Socket clientSocket = socket.accept();

        DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
        FileOutputStream fout = new FileOutputStream("output.jpg");
        int i;
        while ( (i = dis.read()) > -1) {
            fout.write(i);
        }

        DataOutputStream outToClient= new DataOutputStream(clientSocket.getOutputStream());
        outToClient.writeBytes("Hello");
        fout.flush();
        fout.close();
        dis.close();
        clientSocket.close();

}

}
}

Thank you.

Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87
sqtd
  • 485
  • 1
  • 4
  • 6
  • can u be more precise as to what YOU WANT – Terril Thomas Oct 20 '12 at 12:40
  • I can not send the photo to the server – sqtd Oct 20 '12 at 12:44
  • Start with trying "localhost" instead of "hostname" – Egor Oct 20 '12 at 12:44
  • I changed the name to make it clear ... in my application I put the name of the computer running the server program.That not is the problem. – sqtd Oct 20 '12 at 12:48
  • sorry plz dont commit my edit .. i did some mistakes – Sandeep P Oct 20 '12 at 12:55
  • Please be more precise what exactly is not working. Are there any exceptions? Do you receive *some* data on the server? Has the file even been written to the external storage? Btw, you should create a [temporary file in](http://www.exampledepot.com/egs/java.io/CreateTempFile.html) [the (external) cache dir](http://developer.android.com/guide/topics/data/data-storage.html#ExternalCache). Android will take care of deleting it at least on uninstallation, when using `File.createTempFile()` probably even earlier. – cimnine Oct 20 '12 at 13:18
  • the photo is taken and saved on smartphone. On the server does not get anything. I think that in onActivityResult don't find the file. – sqtd Oct 20 '12 at 13:24
  • does the app crash or get exeptions? Or just nothing happens. Makes a lot of difference. – Stephan Celis Nov 12 '12 at 12:15

1 Answers1

0

Don't harcode the filename in the fileoutputstream. Most likely it is incorrect.

Use

Uri uri = data.getData();

to get the uri of the picture taken. "data" Is the Intent parameter of the onActivityResult method.

OR use:

String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();

//THIS IS WHAT YOU WANT!
String capturedImageFilePath = cursor.getString(column_index_data);
Stephan Celis
  • 2,492
  • 5
  • 23
  • 38