在Android中将图像上传到服务器时获取未找到图像

时间:2018-12-24 05:16:27

标签: android android-studio file-upload loopj

我尝试使用请求Params。当我使用JSONONJECT时,它给出了完美的路径,但是当我使用请求params时,却得到了“ FILE”而不是路径。请帮我解决这个问题。下面是我的代码。谢谢。我收到的响应不像图像。返回的params(id=396&profilephoto=FILE),得到的响应是({"status":0,"error":"image file not there"})

private void takePicture() {
    mTask = new GetCounters();
    mTask.execute();
    final CharSequence[] items = {"Take Photo", "Choose from Gallery",
            "Delete Photo"};

    AlertDialog.Builder builder = new AlertDialog.Builder(ProfileActivity.this);
    builder.setTitle("Change Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {


            if (items[item].equals("Take Photo")) {
                userChoosenTask = "Take Photo";
                cameraIntent();
            } else if (items[item].equals("Choose from Gallery")) {
                userChoosenTask = "Choose from Library";
                galleryIntent();

            } else if (items[item].equals("Delete Photo")) {
                dialog.dismiss();
                Glide.with(ProfileActivity.this)
                        .load(R.drawable.profile_default_photo)
                        .thumbnail(0.5f)
                        .into(mciv_UserProfile_pic);
            }
        }
    });
    builder.show();

}
private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, Constants.REQUEST_CAMERA);
}
private void galleryIntent() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);//
    startActivityForResult(Intent.createChooser(intent, "Select File"), Constants.SELECT_FILE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == Constants.SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == Constants.REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}
private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    user_img = Methods.SaveImage(thumbnail);
    Log.d("checkimg", user_img);

    mciv_UserProfile_pic.setImageBitmap(thumbnail);
    sendingProfilePic(user_img);

}
private void onSelectFromGalleryResult(Intent data) {

    Bitmap bm = null;
    if (data != null) {
        Uri contentURI = data.getData();
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
            user_img = Methods.SaveImage(bm);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    mciv_UserProfile_pic.setImageBitmap(bm);
    sendingProfilePic(user_img);
}
private void sendingProfilePic(String user_imge) {
    Log.d("checkimg", user_imge);


    JSONObject js = new JSONObject();
    String s = String.valueOf(new File(Environment.getExternalStorageDirectory().getPath() + "/talentbook_images/" + user_imge));
    Log.d("checksfg", s);//Here I am getting path


    RequestParams jsonParams = new RequestParams();

    try {

        jsonParams.put("id", AppSharedPreferences.getString(Constants.USER_ID));
        if (!user_imge.isEmpty())
            jsonParams.put("profilephoto", new File(Environment.getExternalStorageDirectory().getPath() + "/talentbook_images/" + user_imge));
  //Here it is showing only FILE
        StringEntity entity  = new StringEntity(jsonParams.toString());
        entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8"));
        invokeProfilePic(entity);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.d("checkparam", jsonParams.toString());

}
public void invokeProfilePic(StringEntity entity) {
    AsyncHttpClient client = new AsyncHttpClient();
    client.addHeader("format", "application/x-www-form-urlencoded");

    Log.d("checkparam", Constants.UserConstants.EDIT_PROFILE_PIC);
    client.post(this,url, entity, "application/x-www-form-urlencoded", new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            Log.d("checkresponse", response.toString());
        }


        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            // called when response HTTP status is "4XX" (eg. 401, 403, 404)
            Log.d("Failed: ", "" + statusCode);
            Log.d("Error : ", "" + throwable);
        }
    });
}

0 个答案:

没有答案