Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
位图从Android发送到服务器后不绘制_Android_Android Bitmap_Mediastore - Fatal编程技术网

位图从Android发送到服务器后不绘制

位图从Android发送到服务器后不绘制,android,android-bitmap,mediastore,Android,Android Bitmap,Mediastore,这一次我的头撞到了墙上 在我的Android应用程序中,我选择了一个图像。一切似乎都很顺利。然后我将其转换为字节数组,并通过HTTP将其发送到服务器。在服务器端写入文件的数据似乎与数据开始时不同 端到端检查时,我设备上的文件似乎是1066896字节。服务器上的数据为14745600字节 选择图像的代码: public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requ

这一次我的头撞到了墙上

在我的Android应用程序中,我选择了一个图像。一切似乎都很顺利。然后我将其转换为字节数组,并通过HTTP将其发送到服务器。在服务器端写入文件的数据似乎与数据开始时不同

端到端检查时,我设备上的文件似乎是1066896字节。服务器上的数据为14745600字节

选择图像的代码:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case PICK_IMAGE: {
            if (resultCode != RESULT_OK)
                return;

            Uri selectedImage = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);

                MainActivityFragment fragment = (MainActivityFragment)Fragments.Get("Main");

                new SendPic().execute(bitmap);
            } catch (IOException exception) {
                Log.e(TAG, "Could not load image", exception);
            }

            break;
        }
    }
}
如果我在上面的代码中检查bitmap.getByteCount,它将报告14745600,这使我相对确信发送到服务器的数据是好的。但我无法确定读取的数据是否比我在设备上显示的数据大14倍。如果我使用相同的位图,并将其应用于ImageView,则图像看起来很好

SendPic是一个扩展AsyncTask的简单类。没有魔法

public static String SendHttpImage(String path, Bitmap bitmap, String name, String filename) {
    String attachmentName = "bitmap";
    String attachmentFileName = "bitmap.bmp";
    String crlf = "\r\n";
    String twoHyphens = "--";
    String boundary = "******";

    try {
        String url = "http://server.com/index.php?" + path;
        URL u = new URL(url);
        HttpURLConnection urlConnection = (HttpURLConnection)u.openConnection();
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Connection", "Keep-Alive");
        urlConnection.setRequestProperty("Cache-Control", "no-cache");
        urlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        DataOutputStream request = new DataOutputStream(urlConnection.getOutputStream());

        request.writeBytes(twoHyphens + boundary + crlf);
        request.writeBytes("Content-Disposition: form-data; name=\"" + attachmentName + "\";filename=\"" + attachmentFileName + "\"" + crlf);
        request.writeBytes(crlf);

        int bytes = bitmap.getByteCount();
        ByteBuffer buffer = ByteBuffer.allocate(bytes);
        bitmap.copyPixelsToBuffer(buffer);
        byte[] pixels = buffer.array();

        request.write(pixels);

        request.writeBytes(crlf);
        request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);

        request.flush();
        request.close();

        InputStream responseStream = new BufferedInputStream(urlConnection.getInputStream());
        BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
        String line = "";
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = responseStreamReader.readLine()) != null) {
            stringBuilder.append(line).append("\n");
        }
        responseStreamReader.close();
        String response = stringBuilder.toString();
        urlConnection.disconnect();
        return response;
    } catch (IOException exception) {
        Log.e (TAG, "HTTP Post failed", exception);
        return null;
    }
}

谢谢你在这方面的帮助

非常感谢@Colin Gillespie。在发送到服务器之前,我将数据压缩为jpeg格式,而另一方面,它的效果非常完美。我仍然很困惑,为什么我的浏览器不能显示原始位图,但我不想看到一个礼物马在嘴上。

好的文件将被压缩为png或jpeg等,你正在发送一个原始位图到服务器。我明白了。我一直在想这件事。如果有一个隐式转换到位图正在那里进行。我想这意味着是的。从理论上讲,虽然位图在服务器上应该是可见的,但是效果很好。但是,在发送到服务器之前,我将尝试压缩,看看会发生什么。MediaStore.Images.Media.getBitmap-您正在转换为位图。除非文件已经是原始位图,否则它将更改。如果重新压缩,则必须使用创建文件时使用的压缩参数。在有损格式的情况下,例如jpeg,您将永远无法获得相同的输出。服务器上的文件是否完全相同重要吗?如果没有,我就用jpeg压缩来保存数据。