帮助重构加载位图(而不是文件)到 PHP 服务器的工作方法

发布于 2024-09-28 15:50:49 字数 2346 浏览 3 评论 0原文

这对于文件来说非常有用。我标记了需要重构的位置以使其能够与位图一起使用。我似乎找不到 .addPart 的兼容 ContentBody。

我试图解决的问题是,我已经在 Activity 的 ImageView 中拥有图像,因此我不想从 SD cara 再次读取它,而是想将其直接传递给此方法。当我这样做的时候,我可能会扩展它。

我在这段代码中找到了一条线索,但它似乎不符合 Android

    Part[] parts = {
            new FilePart("pictureFile", new 

ByteArrayPartSource("pictureFile", getBytes(位图))), };

我非常迷失:(

有什么想法吗?TIA。

public JSONArray uploadUserPhoto(String fileToSend,  boolean isPrimary) {

        String url = "http://www.nationwidearbitrationandmediation.com/ia/android/uploadImage.php"; 
        JSONArray jsonArray = null;

       try {
             HttpClient client = new DefaultHttpClient();  
             client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

             HttpPost post = new HttpPost(url);
             //try adding these
             post.getParams().setParameter("http.protocol.expect-continue", false);
             post.getParams().setParameter("http.protocol.content-charset", "UTF-8"); 
             post.getParams().setParameter("http.protocol.reject-relative-redirect", false);

             MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  

             reqEntity.addPart("ip", new StringBody(Boolean.toString(isPrimary), "text/plain", Charset.forName( "UTF-8" )));

               /***  NEED HELP HERE ****/
             File fileToUpload = new File(fileToSend);          //get file from name
             Log.d(TAG,"FileSize: " + fileToUpload.length());   //good time to test if file is larger than maxFileSize


             FileBody fileBody = new FileBody(fileToUpload, "image/jpg");
             reqEntity.addPart("uploadedfile", fileBody);
             /*****  DONE *****/

             post.setEntity(reqEntity);  

             HttpResponse response = client.execute(post);  
             HttpEntity resEntity = response.getEntity(); 
             String serverResponse = EntityUtils.toString(resEntity);
             client.getConnectionManager().shutdown();

             Log.i("RESPONSE",serverResponse);

             return  new JSONArray(serverResponse);

    } catch (Exception e) {
        Log.i(TAG,"Error: " + e.toString());
        return jsonArray;
    }

} //uploadUserPhoto

This works great for files. I marked the location needing refactoring to get it to work with a bitmap. I can't seem to find a compatible ContentBody for .addPart.

The problem I am trying to solve is that I already have the image in an ImageView within an Activity, so rather than read it again from the SD cara, I want to pass it to this method directly. I will probably scale it while I am at it.

I found a clue in this code, however it does not appear to be Android compliant

    Part[] parts = {
            new FilePart("pictureFile", new 

ByteArrayPartSource("pictureFile",
getBytes(bitmap))),
};

I'm so freak'n lost :(

Any Ideas? TIA.

public JSONArray uploadUserPhoto(String fileToSend,  boolean isPrimary) {

        String url = "http://www.nationwidearbitrationandmediation.com/ia/android/uploadImage.php"; 
        JSONArray jsonArray = null;

       try {
             HttpClient client = new DefaultHttpClient();  
             client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

             HttpPost post = new HttpPost(url);
             //try adding these
             post.getParams().setParameter("http.protocol.expect-continue", false);
             post.getParams().setParameter("http.protocol.content-charset", "UTF-8"); 
             post.getParams().setParameter("http.protocol.reject-relative-redirect", false);

             MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  

             reqEntity.addPart("ip", new StringBody(Boolean.toString(isPrimary), "text/plain", Charset.forName( "UTF-8" )));

               /***  NEED HELP HERE ****/
             File fileToUpload = new File(fileToSend);          //get file from name
             Log.d(TAG,"FileSize: " + fileToUpload.length());   //good time to test if file is larger than maxFileSize


             FileBody fileBody = new FileBody(fileToUpload, "image/jpg");
             reqEntity.addPart("uploadedfile", fileBody);
             /*****  DONE *****/

             post.setEntity(reqEntity);  

             HttpResponse response = client.execute(post);  
             HttpEntity resEntity = response.getEntity(); 
             String serverResponse = EntityUtils.toString(resEntity);
             client.getConnectionManager().shutdown();

             Log.i("RESPONSE",serverResponse);

             return  new JSONArray(serverResponse);

    } catch (Exception e) {
        Log.i(TAG,"Error: " + e.toString());
        return jsonArray;
    }

} //uploadUserPhoto

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2024-10-05 15:50:49

解决小图像 - 我使用 base64 编码使图像成为字符串。由于我只想要小图像,这对我有用。

我不会将此标记为已回答,因为其他人可能需要真正的修复而不是破解。

SOLVED FOR SMALL IMAGES - I used base64 encoding to make image a string. Since I only wanted small images this will work for me.

I am not marking this as answered because others might need a true fix and not a hack.

尝蛊 2024-10-05 15:50:49

这是一个修改版本,它将字节数组发送到服务器,只要您可以获得图像字节,您就可以使用它将它们作为 POST 请求的附加图像发送到服务器:

public String uploadUserPhoto(byte[] bytesToSend,  boolean isPrimary) {
    String url = "test.php"; 

    try {
        HttpClient client = new DefaultHttpClient();  
        client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpPost post = new HttpPost(url);
        //try adding these
        post.getParams().setParameter("http.protocol.expect-continue", false);
        post.getParams().setParameter("http.protocol.content-charset", "UTF-8"); 
        post.getParams().setParameter("http.protocol.reject-relative-redirect", false);

        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  

        reqEntity.addPart("ip", new StringBody(Boolean.toString(isPrimary), "text/plain", Charset.forName( "UTF-8" )));

        reqEntity.addPart(new FormBodyPart("uploadedFile", new ByteArrayBody(bytesToSend, "application/octet-stream", "uploadedFile"))); // you can also use image/jpg as mime type

        post.setEntity(reqEntity);  

        HttpResponse response = client.execute(post);  
        HttpEntity resEntity = response.getEntity(); 
        String serverResponse = EntityUtils.toString(resEntity);
        client.getConnectionManager().shutdown();


        return serverResponse;
    } catch (Exception e) {
        System.out.println("Error: " + e.toString());
        return null;
    }
}

Here is a modified version that sends a byte array to the server, as far as you can get the image bytes you can use this to send them to the server as an attached image to the POST request:

public String uploadUserPhoto(byte[] bytesToSend,  boolean isPrimary) {
    String url = "test.php"; 

    try {
        HttpClient client = new DefaultHttpClient();  
        client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

        HttpPost post = new HttpPost(url);
        //try adding these
        post.getParams().setParameter("http.protocol.expect-continue", false);
        post.getParams().setParameter("http.protocol.content-charset", "UTF-8"); 
        post.getParams().setParameter("http.protocol.reject-relative-redirect", false);

        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  

        reqEntity.addPart("ip", new StringBody(Boolean.toString(isPrimary), "text/plain", Charset.forName( "UTF-8" )));

        reqEntity.addPart(new FormBodyPart("uploadedFile", new ByteArrayBody(bytesToSend, "application/octet-stream", "uploadedFile"))); // you can also use image/jpg as mime type

        post.setEntity(reqEntity);  

        HttpResponse response = client.execute(post);  
        HttpEntity resEntity = response.getEntity(); 
        String serverResponse = EntityUtils.toString(resEntity);
        client.getConnectionManager().shutdown();


        return serverResponse;
    } catch (Exception e) {
        System.out.println("Error: " + e.toString());
        return null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文