2012-03-12 3 views
0

me로 사진을 업로드하고 내 친구는 안드로이드 애플리케이션에서 PHP의 Codeigniter 프레임 워크로 사진을 업로드 할 때 문제가 있습니다. 문제는 이미지 파일이 아니기 때문에 이미지 작업에 사용할 수 없기 때문에 발생합니다 (우리는 생각합니다).Android Java 애플리케이션에서 Codeigniter

큰 이미지 중 하나는 이미지 업로드가 html 페이지로 테스트 될 때 작동한다는 것입니다. 그리고 다른 모든 데이터 (이메일과 같은)는 안드로이드 애플리케이션에서 제공되고 작동합니다. 파일 업로드 만 작동하지 않는 유일한 방법입니다. 하지만 그것은 다른 PHP 파일과 함께 작동하지만 내가 가지고있는 질문은 어떻게 작동하고 그것을 codeigniter로 변형시키는 마지막 PHP 스크립트를 가져 가야합니까? 이 프레임 워크를 사용하고 싶지만 지금까지는 할 수 없었습니다. 여기

업로드에 대한 javacode입니다 - 업로드>

HttpClient httpClient = new DefaultHttpClient(); 
HttpContext httpContext = new BasicHttpContext(); 
HttpPost httpPost = new HttpPost("http://url.com/controller-name"); 

try 
{ 
    CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(new ProgressListener() 
    { 
    @Override 
    public void transferred(long num) 
    { 
     publishProgress((int) ((num/(float) totalSize) * 100)); 
    } 
    }); 

    // We use FileBody to transfer an image 
    multipartContent.addPart("userfile", new FileBody(new File(filename))); 
    totalSize = multipartContent.getContentLength(); 
    multipartContent.addPart("email", new StringBody("[email protected]")); 
    multipartContent.addPart("submit", new StringBody("upload")); 

    // Send it 
    httpPost.setEntity(multipartContent); 
    HttpResponse response = httpClient.execute(httpPost, httpContext); 
    String serverResponse = EntityUtils.toString(response.getEntity()); 

    Log.i("SERVER", "Response: " + response.toString()); 
    return serverResponse; 
} 

catch (Exception e) 
{ 
    System.out.println(e); 
} 

된답니다 방법 :

function do_upload() { 
    $image_name = time() . $this->get_random_string(); 
    $config = array(
     'file_name' => $image_name, 
     'allowed_types' => 'jpg|jpeg|gif|png', 
     'upload_path' => $this->gallery_path 

    ); 
    $this->load->library('upload', $config); 
    $this->upload->do_upload(); 
    $image_data = $this->upload->data(); 
    $config = array(
     'source_image' => $image_data['full_path'], 
     'new_image' => $this->gallery_path . '/thumbs', 
     'maintain_ratio' => false, 
     'width' => 300, 
     'height' => 300 
    ); 

    $this->load->library('image_lib', $config); 
    $this->image_lib->resize(); 
    echo $this->image_lib->display_errors(); 

    $image_name = $image_name . $image_data['file_ext']; 
    //Insert into the database 
    $data = array(
     'image_name' => $image_name, 
     'upload_email' => $this->input->post('email'), 
     'ip' => $this->input->ip_address() 
    ); 

    // Insert the new data of the image! 
    $insert = $this->db->insert('table', $data); 
    $short_url = $this->alphaID($this->db->insert_id()); 

    return $short_url; 

} 

작동 오래된 PHP 스크립트, 통지 주석 코드가 작동하지 않는 것을. 아마 CI와 같은 효과를 낼 것이기 때문입니다.

<?php 
/*if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/jpg") 
|| ($_FILES["file"]["type"] == "image/png") 
|| ($_FILES["file"]["type"] == "application/octet-stream") 
|| ($_FILES["file"]["type"] == "image/pjpeg")) 
&& ($_FILES["file"]["size"] < 20000)) 
{*/ 
    if ($_FILES["file"]["error"] > 0) 
    { 
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
    } 
    else 
    { 
     echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br />"; 
     echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />"; 
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 

     if (file_exists("images/" . $_FILES["file"]["name"])) 
     { 
      echo $_FILES["file"]["name"] . " already exists. "; 
     } 
     else 
     { 
      move_uploaded_file($_FILES["file"]["tmp_name"], 
       "images/" . $_FILES["file"]["name"]); 
      echo "Stored in: " . "images/" . $_FILES["file"]["name"]; 
     } 
    } 
/*} 
else 
{ 
    echo "Invalid file"; 
}*/ 
?> 

우리가 사랑하는 이미지를 업로드 할 수 있도록 CI 기능 또는 자바 코드를 수정하는 데 도움을주십시오. 조언과 더 나은 지혜를 주셔서 감사합니다!

+1

echo $ this-> upload-> display_errors();를 추가하면 응용 프로그램에서 파일을 선택하지 않은 것처럼 "선택한 파일 없음"을 출력합니다 시작! : ( – Ms01

+0

자바 코드가 조금 바뀌었고 다음과 같이 표시됩니다. http://pastie.org/private/v7dpbdofhfn97znggslha – Ms01

답변

4

답변은 자바 코드에 있습니다. 필자가 파일 바디에 더 많은 매개 변수를 추가해야했습니다.

multipartContent.addPart("userfile", new FileBody(new File(filename), filename, "image/jpeg", "utf-8")); 

이것은 완벽하게 작동합니다. 여기에서 찾을 수있는 파일 바디 생성자에 대한 정보는 다음과 같습니다. http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/FileBody.html

관련 문제