2012-03-05 63 views
5

Tôi về cơ bản là tring để tải lên hình ảnh từ android và tải nó lên máy chủ php nhưng ở đây tôi không nhận được bất kỳ kết nối nào với mã này hoặc hình ảnh tải lên
. Tôi nhận được lỗi này.Làm thế nào để tải hình ảnh lên máy chủ Php và lưu trữ trong phpmyadmin

Error in http connection java.net.UnknownHostException: host name 

nhưng theo kiến ​​thức của tôi, tôi cũng đã cung cấp đúng kết nối và tệp php trong miền chính xác. Nhìn vào mã của tôi: UploadImage.java

public class UploadImage extends Activity { 
InputStream inputStream; 
    @Override 
public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 

     Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. 
     byte [] byte_arr = stream.toByteArray(); 
     String image_str = Base64.encodeBytes(byte_arr); 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

     nameValuePairs.add(new BasicNameValuePair("image",image_str)); 

     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://server.com/uploadimage/uploadimage.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      String the_string_response = convertResponseToString(response); 
      Toast.makeText(UploadImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show(); 
     }catch(Exception e){ 
       Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show(); 
       System.out.println("Error in http connection "+e.toString()); 
     } 
    } 

    public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{ 

     String res = ""; 
     StringBuffer buffer = new StringBuffer(); 
     inputStream = response.getEntity().getContent(); 
     int contentLength = (int) response.getEntity().getContentLength(); //getting content length….. 
     Toast.makeText(UploadImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show(); 
     if (contentLength < 0){ 
     } 
     else{ 
       byte[] data = new byte[512]; 
       int len = 0; 
       try 
       { 
        while (-1 != (len = inputStream.read(data))) 
        { 
         buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer….. 
        } 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
       try 
       { 
        inputStream.close(); // closing the stream….. 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
       res = buffer.toString();  // converting stringbuffer to string….. 

       Toast.makeText(UploadImage.this, "Result : " + res, Toast.LENGTH_LONG).show(); 
       //System.out.println("Response => " + EntityUtils.toString(response.getEntity())); 
     } 
     return res; 
    } 

}

Mã Php:

<?php 
$base=$_REQUEST['image']; 
$binary=base64_decode($base); 
header('Content-Type: bitmap; charset=utf-8'); 
$file = fopen('uploaded_image.jpg', 'wb'); 
fwrite($file, $binary); 
fclose($file); 
echo 'Image upload complete!!, Please check your php file directory……';?> 

Bất kỳ một tiếng vấn đề này? nếu có ai biết làm thế nào để lưu trữ trong cơ sở dữ liệu mysql từ file php và lấy viceversa xin đề nghị tôi ở đây ...

+1

Nếu bạn đang thử nghiệm trên giả lập, khi khởi động lại giả lập của bạn đôi khi nó được đưa ra ngoại lệ này nhưng sau khi khởi động lại nó sẽ làm việc tốt. Bạn cũng có thể kiểm tra kết nối internet bằng cách mở trình duyệt sẵn có của trình mô phỏng. – Dharmendra

Trả lời

8

Vấn đề là rất rõ ràng ...

Error in http connection java.net.UnknownHostException: host name 

nghĩa là HttpPost không thể đưa ra một kết nối sử dụng tên máy chủ bạn đã cung cấp - bởi vì tên máy chủ bạn cung cấp không được biết.

Nếu bạn lấy hostname từ dòng này:

HttpPost httppost = new HttpPost("http://server.com/uploadimage/uploadimage.php"); 

và đặt nó trong một trình duyệt trên cùng một thiết bị điều gì xảy ra ... tôi đề nghị bạn sẽ nhận được một lỗi nói rằng không thể kết nối để lưu trữ. Nếu đây làm việc sau đó tôi đề nghị bạn kiểm tra các dòng sau là biểu hiện của bạn:

<uses-permission android:name="android.permission.INTERNET" /> 

Cũng đảm bảo rằng các file PHP chứa các tiêu đề sau đây nếu bạn sử dụng một JPEG:

header('Content-Type: image/jpg'); 
+0

Tôi nhận được tín hiệu chính xác khi tôi sử dụng dòng này trong trình duyệt và hiện tại nhận được độ dài nội dung = -1 .. bạn có thể thấy y wat để làm tiếp theo không. – Rizvan

1

Kiểm tra cấu hình máy chủ và chọn tiêu đề phải để tải lên tệp. Trong mã php của bạn, bạn đã đưa ra loại tiêu đề sai.

1

Tôi khuyên bạn nên sử dụng ManseUK để thêm quyền trong Tệp kê khai của bạn. Lỗi này là khá rõ ràng nhưng thường được giải quyết bằng cách thêm <uses-permission android:name="android.permission.INTERNET" />

3
1. Need to add Internet permission in android manifest file . 
<uses-permission android:name="android.permission.INTERNET" /> 

2. Every Time i used to see image using url but unable to see because i didnt added 
     echo "<img src=test.jpg>"; 
3.$file = fopen('test.jpg', 'wb'); 

4. final thing is i have to change header file as : 

header('Content-Type: image/jpg; charset=utf-8'); 
0

này làm việc cho tôi:

// thay đổi định dạng nén bitmap để jpeg
bitmap.compress (Bitmap.CompressFormat.JPEG, 100, baos);

// sau đó uploadimage.php

// lưu ý imagecreatefromstring() chức năng

error_reporting(E_ALL); // in case its turned off and your not seeing errors 

ini_set('display_errors','1'); // confirm and browse to page 

if($base) { 
    $ttime = intval(time()); 
    $quality = '100'; 
    $save_to = 'images/img-' . $ttime . '.jpeg'; 
    $binary=base64_decode($base); 
    $im = imagecreatefromstring($binary); 
    if ($im !== false) { 
     header('Content-Type: image/jpg'); 
     $idno = ImageJPEG($im, $save_to, $quality); 
     imagedestroy($im); 
     echo "iid:" . $ttime; 
    } else { 
     echo "Error:" . $ttime; 
    } 
} 
Các vấn đề liên quan