Android使用改进后发布方法将照片发送到服务器

时间:2017-08-22 09:45:26

标签: android android-image android-networking

嗨伙计们首先我是android和堆栈溢出的新手。我需要的是从相机捕获图像并发送到php服务器。

用于拍摄我使用的图像

    Intent photo= new Intent("android.media.action.IMAGE_CAPTURE");
            startActivityForResult(photo, 0);

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0 && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
         imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
        delete.setVisibility(View.VISIBLE);
        button.setVisibility(View.INVISIBLE);
        image = ConvertBitmapToString(imageBitmap);

    }
}

//method to convert the selected image to base64 encoded string//////////////
public String ConvertBitmapToString(Bitmap bitmap){

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 25, byteArrayOutputStream);

    String encodedImage= null;
    try {
        encodedImage = URLEncoder.encode(Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return encodedImage;
}

我的问题是图像在服务器中不可见。

服务器连接

      public interface SaveAPI {
     @FormUrlEncoded
    @POST("/example.php")
public void insertUser(
        @Field("username") String name,
        @Field("coname") String cname,
        @Field("location") String location,
        @Field("ddate") String date,
        @Field("dracc") String daccnt,
        @Field("cracc") String cccnt,
        @Field("amount") String samt,
        @Field("narr") String snarration,
        @Field("bill") String image,
        Callback<Response> callback);

     }
   private void insertUser() {
    //Here we will handle the http request to insert user to mysql db
    //Creating a RestAdapter
        RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(SAVE_URL) //Setting the Root URL
            .build();
       SaveAPI api = adapter.create(SaveAPI.class);
       api.insertUse r(
            name,
            sCname.getSelectedItem().toString(),
            loc,
            tdate,
            sdamount.getSelectedItem().toString(),
            scamount.getSelectedItem().toString(),
            amt.getText().toString(),
            narration.getText().toString(),
            image,
            new Callback<Response>() {
                @Override
                public void success(Response result, Response response) {
                    //On success we will read the server's output using 
            bufferedreader
                    //Creating a bufferedreader object
                    BufferedReader reader = null;

                    //An string to store output from the server
                    String output = "";

                    try {
                        //Initializing buffered reader
                        reader = new BufferedReader(new 
                    InputStreamReader(result.getBody().in()));

                        //Reading the output in the string
                        output = reader.readLine();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    //Displaying the output as a toast
                    Toast.makeText(ExpenseActivity.this, output, 
              Toast.LENGTH_LONG).show();
                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(ExpenseActivity.this, error.toString(), 
               Toast.LENGTH_LONG).show();

                }

              }
          );
        }

实际上我正在向服务器发送9个数据,除了图像全部显示在服务器中,任何人都可以解决问题。提前谢谢

3 个答案:

答案 0 :(得分:1)

现在我的图像对话完美无缺........

    public String ConvertBitmapToString(Bitmap bitmap)throws 
    FileNotFoundException {
    BitmapFactory.Options options = null;
    options = new BitmapFactory.Options();
    options.inSampleSize = 3;
    ByteArrayOutputStream byteArrayOutputStream = new 
    ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,40, byteArrayOutputStream);
    byte[] byteArrayImage = byteArrayOutputStream.toByteArray();
    String encodedImage = 
    Base64.encodeToString(byteArrayImage,Base64.DEFAULT);
    return encodedImage;
   }

答案 1 :(得分:0)

首先检查转换后的图像是否为返回数据,并且该响应是您希望再次将其转换为图像的有效响应。

答案 2 :(得分:0)

首先,调试应用程序,并检查图像是否转换为base64。如果它正确转换并将其发送到您的服务器,则问题出在您的服务器服务器中。调试你的项目,并告诉你发生了什么。请将您绑定数据的服务器代码部分