【问题标题】:Opening a PNG File from Memory and Send Through TCP Sockets从内存中打开 PNG 文件并通过 TCP 套接字发送
【发布时间】:2019-02-06 13:41:31
【问题描述】:

我希望将图像从我的 android 设备发送到运行 python 的计算机/树莓派。我已经使用以下代码从我的 Windows 笔记本电脑(运行 java 客户端)实现了通信。

BufferedImage image = ImageIO.read (new File("C:/Users/****/OneDrive/Pictures/Om-nom.png"));
s = new Socket("192.168.2.69",8000);
ImageIO.write(image, "png", s.getOutputStream());
s.close();

但是,Android 不支持 BufferedImage 库 (java.awt.image)。如何在 android studio 中实现类似的功能以在我的 android 设备上运行,即将设备内存中的 PNG 文件读取到字节缓冲区中,然后将其发送到服务器 PC。

注意:我的文件的位置类似于以下/storage/emulated/0/Downloads/frog-face_1f438.png

【问题讨论】:

    标签: android inputstream android-storage


    【解决方案1】:

    要获取图像,您可以这样做:

        // TODO check sdcard is available
        File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File photoPath = new File(directory, "temp.jpg");
    
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        final Bitmap bitmap = BitmapFactory.decodeFile(photoPath.getAbsolutePath(), options);
    

    要发送到您的服务器,您可以使用 okHttp 客户端或您使用哪种协议与服务器通信。

    更新: 在清单中你应该指出这个权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    如果您在 android 6 及更高版本上运行代码,您应该处理运行时权限 (WRITE_EXTERNAL_STORAGE):

    List of Android permissions normal permissions and dangerous permissions in API 23?

    // oncreate method or whereever you want to call your code 
            if (ContextCompat.checkSelfPermission(this, 
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        STATUS_CODE);
            } else {
                // TODO run your code
            }
    
    
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case STATUS_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // TODO run your code
                } else {
                    // TODO show warning
                }
    

    【讨论】:

    • 我正在使用 TCP 协议将数据传输到 python 服务器。代码看起来不错,但我收到权限错误。有什么可以添加到我的 AndroidMainifest.xml 的吗?谢谢
    • 根据您的评论添加详细信息
    【解决方案2】:

    感谢您的帮助 Gleichmut。如果有人在他们的应用程序中需要类似的功能(通过 TCP 套接字发送图像),我已经设法编写了一些在 GitHub 上可用的工作代码。

    这里的 GitHub 存储库:https://github.com/Lime-Parallelogram/Sending-Images-Through-Sockets-Android-to-Python-

    注意:我在 GitHub 上的代码包括一个 python 服务器和一个用于在 android 上运行的 java 客户端 - 用 Android-Studio 编写

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2012-05-11
      • 2015-07-10
      • 2015-01-30
      • 2020-04-28
      • 2017-03-14
      • 2011-03-16
      相关资源
      最近更新 更多