Android:先上传位图而不保存到文件系统

Android:先上传位图而不保存到文件系统,android,bitmap,upload,Android,Bitmap,Upload,我有两个图像,保存为内存中的位图: private Bitmap image1; private Bitmap image2; 现在我想把这些位图上传到我的服务器上。以下是我目前的工作: 我将图像以PNG格式写入文件系统(例如内部存储),使用URI作为位置。然后我调用此方法使用multipart POST上载文件: private int uploadImageFiles(String image1Uri, String image2Uri) { File image1 = new F

我有两个图像,保存为内存中的位图:

private Bitmap image1;
private Bitmap image2;
现在我想把这些位图上传到我的服务器上。以下是我目前的工作: 我将图像以PNG格式写入文件系统(例如内部存储),使用URI作为位置。然后我调用此方法使用multipart POST上载文件:

private int uploadImageFiles(String image1Uri, String image2Uri) {
    File image1 = new File(image1Uri);
    File image2 = new File(image2Uri);

    try {
         HttpClient client = new DefaultHttpClient();
         HttpPost post = new HttpPost(uploadServerUriNodeJs);

         // additional headers for node.js server
         post.addHeader("ACCEPT", "application/melancholia+json");
         post.addHeader("ACCEPT-VERSION", "1.0");

         // user authorization
         String usernamePassword = USER + ":" + PASSWORD;
         String encodedUsernamePassword = Base64.encodeToString(usernamePassword.getBytes(), Base64.NO_WRAP);
         post.addHeader("AUTHORIZATION", "Basic " + encodedUsernamePassword);

         FileBody bin1 = new FileBody(image1, "image/png");
         FileBody bin2 = new FileBody(image2, "image/png");
         MultipartEntity reqEntity = new MultipartEntity();
         reqEntity.addPart("image1", bin1);
         reqEntity.addPart("image2", bin2);
         post.setEntity(reqEntity);
         HttpResponse response = client.execute(post);
         HttpEntity resEntity = response.getEntity();
         final String response_str = EntityUtils.toString(resEntity);
         if (resEntity != null) {
             return getIdFromResponse(response_str);
         }
    }
    catch (Exception ex){
         Log.e("Debug", "error: " + ex.getMessage(), ex);
    }
    return -1;
}
现在我的问题是:除了上传,我不需要为我的应用程序将这些位图写入文件系统。图像文件的大小可能相当大,因此首先将其写入文件系统可能需要相当长的额外时间,这对性能不利

我想要的是:有没有一种方法可以在不先将位图保存到文件系统的情况下上载位图

顺便说一下,我不允许对服务器进行更改


更新:以下是我解决问题的方法:

此示例使用ApacheHttpClient 4.3.6 在 请参阅本文,了解要包含哪些库: 也不要忘记在“订单和出口”选项卡上检查它们


验证服务器将接受纯POST(而不是mime多部分)

在没有标题的情况下进行游戏(对于此类帖子,您的服务器可能非常依赖于特定的MIME类型)

如果它能工作,那么你可以使用“byteArrayEntity”作为帖子主体的帖子从android上传不同类型的内容


从位图的ByteArray中获取一个流,并将其馈送到输入流中。

是的,您可以上载位图而无需将其保存到文件系统。写入
HttpURLConnection
返回的
OutputStream

private Bitmap bitmap;
HttpURLConnection connection;

    try
    {
        URL url = new URL("urlServer");
        connection = (HttpURLConnection) url.openConnection();

        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;");

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(byteArray);
        outputStream.flush();
        outputStream.close();
    }
    catch (Exception ex)
    {
        //Exception handling
    }
    finally {
        if(connection != null) {
            connection.disconnect();
        }
    }

服务器是一个node.js服务器,目前功能非常有限。一个普通的职位可能不起作用。我已经尝试了不同的上传解决方案,需要几个小时才能让它正常工作。没有任何解决方案可以让我从位图中创建某种“假文件”或“假文件体”,并使用上面写的方法吗?谢谢,但请查看我上面的评论:
curl -X POST "https://youserver/path --header "Transfer-Encoding: chunked" --header "Content-Type: pic/*;"  --data-binary @Your-photo
private Bitmap bitmap;
HttpURLConnection connection;

    try
    {
        URL url = new URL("urlServer");
        connection = (HttpURLConnection) url.openConnection();

        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;");

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(byteArray);
        outputStream.flush();
        outputStream.close();
    }
    catch (Exception ex)
    {
        //Exception handling
    }
    finally {
        if(connection != null) {
            connection.disconnect();
        }
    }