안드로이드 카메라로 찍은 사진 서버에 업로드 중 오류가 발생합니다

조회수 1366회

안녕하세요! php는 처음 다뤄보고 안드로이드를 한창 공부중인 학생입니다! 카메라로 찍은 사진이나 갤러리에서 받아온 사진의 uri를 서버로 올리는 안드로이드 어플을 만들고 있는데요. 갤러리에서 선택한 사진은 서버에 잘 저장이 되는데 카메라로 찍은 사진은 자꾸 오류가 납니다. Other Error : End of input at character 0 of 이라는 에러가 잡힙니다.

아래는 upload.php 코드입니다.

<?php

if(isset($_POST['title']) && $_POST['title']>0) {
    $file_path = "img/".$_POST['title'];
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$file_path)) {

        $result = array("result" => "success");
    } else {
        $result = array("result" => "error");
    }

    $filename = $_POST['title'];
    $upload_time = $_POST['upload_time'];
    $img_url = "http://14.55.142.139/".$file_path;
    $userID = $_POST['userID'];

    include "./db/dbconn.php";

    $sql = "INSERT INTO images SET ";
    $sql .= " `title`='".$filename."'";
    $sql .= ", `upload_time`='".$upload_time."'";
    $sql .= ", `img_url`='".$img_url."'";
    $sql .= ", `userID`='".$userID."'";
    $sql .= ";";

    mysqli_query($connect, $sql);
    mysqli_close($connect);

    echo json_encode($result);
}
?>

안드로이드스튜디오에서 업로드 해주는 부분 코드입니다.

    public JSONObject f_uploadImage(String UPLOAD_SERVER_URL, String m_photoPath) {
        try {
            File sourceFile = new File(m_photoPath);

            final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/*");

            String filename = m_photoPath.substring(m_photoPath.lastIndexOf("/") + 1);

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
            Calendar calendar = Calendar.getInstance();
            String savetime = dateFormat.format(calendar.getTime());

            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("uploaded_file", filename, RequestBody.create(MEDIA_TYPE_PNG, sourceFile))
                    .addFormDataPart("title", filename)
                    .addFormDataPart("upload_time", savetime)
                    .addFormDataPart("userID", userID)
                    .build();


            Request request = new Request.Builder()
                    .url(UPLOAD_SERVER_URL)
                    .post(requestBody)
                    .build();

            OkHttpClient client = new OkHttpClient();
            Response response = client.newCall(request).execute();
            String res = response.body().string();
            Log.v("MYTAG", res);
            return new JSONObject(res);

        } catch (UnknownHostException | UnsupportedEncodingException e) {
            Log.e("MYTAG", "Error : " + e.getLocalizedMessage());
        } catch (Exception e) {
            Log.e("MYTAG", "Other Error : " + e.getLocalizedMessage());
        }
        return null;
    }

갤러리에서 선택해서 가져올 때는 문제가 없는데 찍은 사진에서 문제가 발생하니 어느 부분이 문제인지 모르겠어요 디버그를 해봐도 filename이랑은 다 정상적으로 들어가는데 어느 부분이 문제인지 봐주실 수 있나요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)