首页 文章

使用带有PHP的Json StringRequest将图像从Android中的库上传到服务器?

提问于
浏览
0

我想将图像上传到服务器,我该怎么做才能在服务器中保存图像 .

我想使用Json的String请求将二进制文件映像的值传递给服务器 . 在服务器端我使用php来保存这样的文件:

$binary=base64_decode($base);
  header('Content-Type: bitmap; charset=utf-8');
   // Images will be saved under 'www/imgupload/uplodedimages' folder
      $file = fopen('../uploadedimages/'.$filename, 'wb');
      // Create File

     fwrite($file, $binary);
     fclose($file);

1 回答

  • 0

    使用Base64 Utility Class将选定的Image转换为String . 使用AsynTask将Image编码为String,因为它是长时间运行的进程 . 编码完成后,调用'triggerImageUpload'方法,该方法将启动图像上传 .

    public void encodeImagetoString() {
            new AsyncTask<Void, Void, String>() {
    
            protected void onPreExecute() {
    
            };
    
            @Override
            protected String doInBackground(Void... params) {
                BitmapFactory.Options options = null;
                options = new BitmapFactory.Options();
                options.inSampleSize = 3;
                bitmap = BitmapFactory.decodeFile(imgPath,
                        options);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Must compress the Image to reduce image size to make upload easy
                bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream); 
                byte[] byte_arr = stream.toByteArray();
                // Encode Image to String
                encodedString = Base64.encodeToString(byte_arr, 0);
                return "";
            }
    
            @Override
            protected void onPostExecute(String msg) {
                prgDialog.setMessage("Calling Upload");
                // Put converted Image string into Async Http Post param
                params.put("image", encodedString);
                // Trigger Image upload
                triggerImageUpload();
            }
        }.execute(null, null, null);
    }
    

    现在使用AsyncHttp库将编码后的字符串通过HTTP发送到PHP服务器 . (您可以使用任何HTTP库) . 编码完成后,在onPostExecute方法中调用方法'triggerImageUpload':

    public void triggerImageUpload() {
            makeHTTPCall();
        }
    

    将编码后的字符串发布到PHP服务器:

    public void makeHTTPCall() {
        prgDialog.setMessage("Invoking Php");        
        AsyncHttpClient client = new AsyncHttpClient();
        // Don't forget to change the IP address to your LAN address. Port no as well.
        client.post("http://192.168.2.5:9000/imgupload/upload_image.php",
                params, new AsyncHttpResponseHandler() {
                    // When the response returned by REST has Http
                    // response code '200'
                    @Override
                    public void onSuccess(String response) {
                        // Hide Progress Dialog
                        prgDialog.hide();
                        Toast.makeText(getApplicationContext(), response,
                                Toast.LENGTH_LONG).show();
                    }
    
                    // When the response returned by REST has Http
                    // response code other than '200' such as '404',
                    // '500' or '403' etc
                    @Override
                    public void onFailure(int statusCode, Throwable error,
                            String content) {
                        // Hide Progress Dialog
                        prgDialog.hide();
                        // When Http response code is '404'
                        if (statusCode == 404) {
                            Toast.makeText(getApplicationContext(),
                                    "Requested resource not found",
                                    Toast.LENGTH_LONG).show();
                        }
                        // When Http response code is '500'
                        else if (statusCode == 500) {
                            Toast.makeText(getApplicationContext(),
                                    "Something went wrong at server end",
                                    Toast.LENGTH_LONG).show();
                        }
                        // When Http response code other than 404, 500
                        else {
                            Toast.makeText(
                                    getApplicationContext(),
                                    "Error Occured \n Most Common Error: \n1. Device not connected to Internet\n2. Web App is not deployed in App server\n3. App server is not running\n HTTP Status code : "
                                            + statusCode, Toast.LENGTH_LONG)
                                    .show();
                        }
                    }
                });
    }
    

    现在根据PHP服务器返回的HTTP响应,通知用户上传状态(上传成功或失败) .

    哇,我们做到了 .

相关问题