简体   繁体   中英

Send an image file using sockets , Android to Python

Im trying to send an image from an android client to a python server , I have achieved it before with a python test client and the server. Basically my client app takes a form consisting of users details and an image. Once the user selects the image I get the uri in the onActivityResult method and parse it to a string Like so

selectedImagePath = selectedImageUri.toString();

when the user hits submit button on the form the sending activity is invoked with the form data as extras in an array in a bundle like so.

Bundle b=new Bundle(); b.putStringArray("regValues", new String[]{name,email,selectedImagePath});

in the sending activity I establish a connection with the server and attempt to send the image like so .

` //establish link with the server

    try{

     //refer to the host computer's loopback interface
     host = InetAddress.getByName("10.0.2.2"); 
     link = new Socket(host,port);
     in = new BufferedReader(new InputStreamReader(link.getInputStream()));

     //access the strings that were passed to this activity 
     Bundle b = this.getIntent().getExtras();
     String[] regFormValues = b.getStringArray("regValues");

     //display connection confirmation

     String message = in.readLine();
     status.setText(message);



        File myFile = new File (regFormValues[2]);
        byte [] mybytearray  = new byte [(int)myFile.length()];
        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(mybytearray,0,mybytearray.length);
        OutputStream os = link.getOutputStream();

        os.write(mybytearray,0,mybytearray.length);
        os.flush();
        link.close();





    }
    catch(IOException e ){

     e.printStackTrace();

    }
    `

It connects to the server no problem , the server creates a file for output to write the data to as usual but it just appears empty so no data is being recieved. I think it may be a problem with the file path on the client side. Any Ideas ?

EDIT: Basically I would like to know if I am accessing the image file in the right way or if you have any better suggestions for accessing and sending it.

Consider using methods such as File.exists() and File.isFile() to validate if the path is OK and whether the image is really there

Also check if the image even hits the wire - using tcpdump ro wireshark

I have solved it , as I use selectedImageUri = Uri.fromFile(photo); to get the uri if its taken by the camera and selectedImageUri = data.getData(); if its been selected by the file browser. I used selectedImagePath = selectedImagePath.substring(7); to strip the file:// form the camera's image path, resulting in a /sdcard/etc or wherever it stored it. To get the file path for the image chosen by the file browser i used this

// convert the image URI to the direct file system path of the image file  
public String getRealPathFromURI(Uri contentUri) {  

    // can post image  
    String [] proj={MediaStore.Images.Media.DATA};  
    Cursor cursor = managedQuery( contentUri,  
            proj, // Which columns to return  
            null,       // WHERE clause; which rows to return (all rows)  
            null,       // WHERE clause selection arguments (none)  
            null); // Order-by clause (ascending by name)  
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
    cursor.moveToFirst();  

    return cursor.getString(column_index);  

Now it works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM