Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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 安卓:通过POST发送图像_Android_Image_Http_Post - Fatal编程技术网

Android 安卓:通过POST发送图像

Android 安卓:通过POST发送图像,android,image,http,post,Android,Image,Http,Post,我一直在寻找解决方案,遇到了多部分和不同的设置,但我似乎无法让它正常工作 Bitmap bitmapOrg = images.get(0); ByteArrayOutputStream bao = new ByteArrayOutputStream(); String upload_url = prepare_upload_url(); bitmapOrg.compress(Bitmap.CompressFormat.JPEG,

我一直在寻找解决方案,遇到了多部分和不同的设置,但我似乎无法让它正常工作

        Bitmap bitmapOrg = images.get(0);

        ByteArrayOutputStream bao = new ByteArrayOutputStream();

        String upload_url = prepare_upload_url();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

        byte[] data = bao.toByteArray();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(upload_url);
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        //Set Data and Content-type header for the image
        entity.addPart("file",
                new ByteArrayBody(data, "image/jpeg", "file"));
        postRequest.setEntity(entity);
        try {

            HttpResponse response = httpClient.execute(postRequest);
        //Read the response
            String jsonString = EntityUtils.toString(response.getEntity());
            Log.v(ProgramConstants.TAG, "after uploading file "
                    + jsonString);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
这是我到目前为止所拥有的

        Bitmap bitmapOrg = images.get(0);

        ByteArrayOutputStream bao = new ByteArrayOutputStream();

        String upload_url = prepare_upload_url();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

        byte[] data = bao.toByteArray();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(upload_url);
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        //Set Data and Content-type header for the image
        entity.addPart("file",
                new ByteArrayBody(data, "image/jpeg", "file"));
        postRequest.setEntity(entity);
        try {

            HttpResponse response = httpClient.execute(postRequest);
        //Read the response
            String jsonString = EntityUtils.toString(response.getEntity());
            Log.v(ProgramConstants.TAG, "after uploading file "
                    + jsonString);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
编辑:我得到的服务器端错误是500。我认为这是因为我发送的数据对于一个请求来说太大或者格式不正确

                    ByteArrayOutputStream bao = new ByteArrayOutputStream();

                    bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);

                    byte [] ba = bao.toByteArray();

                    String ba1=Base64.encodeToString(ba,Base64.URL_SAFE);

                    mParams.add(new BasicNameValuePair("story[image]",ba1));

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(mPath);
    // Add your data
    try
    {
        httppost.setHeader("Authorization", Base64.encodeToString(new StringBuilder(sssss).append(":").append(ssssss).toString().getBytes("UTF-8"), Base64.URL_SAFE|Base64.NO_WRAP));
        httppost.setEntity(new UrlEncodedFormEntity(mParams));
        HttpResponse rH = httpclient.execute(httppost);
        Log.v(TAG, "response: " + rH.toString());
        int f = 0;
    }
    catch(HttpResponseException e)
    {
        Log.e(TAG, e.getLocalizedMessage());
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    }
        Bitmap bitmapOrg = images.get(0);

        ByteArrayOutputStream bao = new ByteArrayOutputStream();

        String upload_url = prepare_upload_url();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

        byte[] data = bao.toByteArray();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(upload_url);
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        //Set Data and Content-type header for the image
        entity.addPart("file",
                new ByteArrayBody(data, "image/jpeg", "file"));
        postRequest.setEntity(entity);
        try {

            HttpResponse response = httpClient.execute(postRequest);
        //Read the response
            String jsonString = EntityUtils.toString(response.getEntity());
            Log.v(ProgramConstants.TAG, "after uploading file "
                    + jsonString);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

这就是我昨天做的,也许会有帮助

        Bitmap bitmapOrg = images.get(0);

        ByteArrayOutputStream bao = new ByteArrayOutputStream();

        String upload_url = prepare_upload_url();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

        byte[] data = bao.toByteArray();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(upload_url);
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        //Set Data and Content-type header for the image
        entity.addPart("file",
                new ByteArrayBody(data, "image/jpeg", "file"));
        postRequest.setEntity(entity);
        try {

            HttpResponse response = httpClient.execute(postRequest);
        //Read the response
            String jsonString = EntityUtils.toString(response.getEntity());
            Log.v(ProgramConstants.TAG, "after uploading file "
                    + jsonString);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

你会在哪里设置帖子信息。我正试图将post中story[image]的值设置为图像数据。啊。我附加了文件名为“file”的图像字节数组。entity.addPart(“文件”,新的ByteArrayBody(数据,“图像/jpeg”,“文件”))我正在使用它将图像发布到App Engine服务器,但是如果您中途中断internet连接,这不就是保存一个损坏的图像吗?嘿!这篇文章发表已经5年了,
MultipartEntity
并不完全可用。有人能提出一个替代方案吗?