使用sockets、Android向Python发送图像文件

使用sockets、Android向Python发送图像文件,python,android,sockets,file,Python,Android,Sockets,File,我试图将一个图像从android客户端发送到python服务器,之前我已经通过python测试客户端和服务器实现了这一点。基本上,我的客户端应用程序采用由用户详细信息和图像组成的形式。一旦用户选择了图像,我就会在onActivityResult方法中获取uri,并将其解析为如下所示的字符串 selectedImagePath=selectedImageUri.toString() 当用户点击表单上的submit按钮时,发送活动将被调用,表单数据将作为捆绑包中的额外数据 Bundle b=新Bun

我试图将一个图像从android客户端发送到python服务器,之前我已经通过python测试客户端和服务器实现了这一点。基本上,我的客户端应用程序采用由用户详细信息和图像组成的形式。一旦用户选择了图像,我就会在onActivityResult方法中获取uri,并将其解析为如下所示的字符串

selectedImagePath=selectedImageUri.toString()

当用户点击表单上的submit按钮时,发送活动将被调用,表单数据将作为捆绑包中的额外数据

Bundle b=新Bundle();
b、 putStringArray(“regValues”,新字符串[]{name,email,selectedImagePath})

在发送活动中,我与服务器建立了连接,并尝试像这样发送图像

` //建立与服务器的链接

    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();

    }
    `
它连接到服务器没有问题 服务器会像往常一样为输出创建一个文件,以便将数据写入其中,但它只是显示为空,所以不会接收任何数据。我认为客户端的文件路径可能有问题。有什么想法吗


编辑:基本上我想知道我是否以正确的方式访问图像文件,或者您是否对访问和发送图像有更好的建议。

考虑使用file.exists()和file.isFile()等方法验证路径是否正确以及图像是否真的存在


还要检查图像是否甚至碰到了电线-使用tcpdump ro wireshark,我已经解决了它,因为我
使用selectedImageUri=Uri.fromFile(照片)
获取uri(如果它是由相机拍摄的),然后
选择edimageri=data.getData()如果其已被文件浏览器选中。我使用了
selectedImagePath=selectedImagePath.substring(7)
将文件从相机的图像路径中剥离,生成/sdcard/etc或其存储位置。为了获得文件浏览器选择的图像的文件路径,我使用了

// 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);  

现在,它的工作如预期

我似乎不是这样,我在客户端上接收到连接到服务器的消息,所以我知道它有那么远,我只是在服务器端得到一个空文件,服务器在使用python客户端发送和接收图像之前已经工作了。我认为这与客户端中的文件有关file.exists()和file.isFile()确认它没有看到该文件。该文件确实存在,因此它必须是我访问它的方式。我目前正在尝试的方法是选择一个文件并返回uri,将其解析为字符串并将字符串传递给发送活动,然后尝试在那里打开文件以发送它。任何关于如何访问该文件的建议您的意思是“File.exists()和File.isFile()确认它确实看到了该文件”我假定您是myFile.isFile()。在bis.read之后,您看到mybytearray中的图像了吗?我没有看到os.flush()之后的os.close()-这可能就是原因。不,它们都返回false。但我知道这些文件存在,因为我可以通过浏览应用程序中的图像来查看它们。我想这是因为我试图用这样的路径打开文件content://media/external/images/media/3 作为字符串。我应该直接使用uri还是使用内容提供者。OK-use-uri-File有一个构造函数-File(uri-uri)使用指定uri的路径构造一个新文件。准备好myFile后,请使用exists或isFile检查它。