2012-03-05 16 views
5

Temel olarak android'den resim yüklemeyi ve php sunucusuna yüklemeyi yapıyorum ama burada bu kodla veya resim yükleme ile bağlantı kuramıyorum
. Bu hatayı alıyorum.Php sunucusuna nasıl resim yüklenir ve phpmyadmin içinde depolanır

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

fakat bilgiime göre doğru bağlantıyı ve php dosyasını doğru etki alanında da verdim. Benim kodu Görünüş: 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; 
    } 

}

Php Kodu: Bu sorunu bilinen

<?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……';?> 

herhangi biri? herhangi bir php dosyasından mysql veritabanında depolamak veya tersine getirmesi nasıl biliyorsanız

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

HttpPost bağlantı kurmak anlamına gelir ... sorun çok açıktır ... Burada

+1

imagecreatefromstring() fonksiyonunu not uploadimage.php güzel çalış. Ayrıca, emülatörün dahili tarayıcısını açarak internet bağlantısını kontrol edebilirsiniz. – Dharmendra

cevap

8

beni önermek lütfen Sağladığınız ana bilgisayar adını kullanarak - sağladığınız ana makine adı bilinmediğinden. Bu hattan makine adını alırsak

:

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

ve ne aynı cihazda bir tarayıcıda koydum ... i barındırmak için bağlanamıyor belirten bir hata alırsınız düşündürmektedir.

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

Ayrıca senin bir JPEG kullanılıyorsa PHP dosyası aşağıdaki başlık içerdiğinden emin:

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

Bu satırı tarayıcımda kullandığımda doğru yankı alıyorum ve şu anda içerik almakta olduğum uzunluk = -1 .. bir sonraki adımı görebileceksiniz .. – Rizvan

1

Kontrol Sunucu yapılandırma ve bu daha sonra çalışırsa i aşağıdaki satırı Manifest'inizde olduğunu kontrol önermek dosya yükleme için doğru başlığı seçin. Php kodunuzda yanlış başlık tipi verdiniz.

1

ManseUK’un Manifest’te izin vermesi gerektiğini söylediğiniz gibi tavsiye ederim. Bu hata oldukça belirsizdir fakat genellikle bu benim için çalışıyor <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

ekleyerek çözülene:

//
bitmap.compress jpeg bitmap kompres biçimini değiştirmek (Bitmap.CompressFormat.JPEG, 100, baos);

// sonra

// Eğer emülatörünüzde test ediyorsanız, bir kez daha bu istisnayı veriyor ancak yeniden başlatma sonrasında o olacak bazen emülatör yeniden

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; 
    } 
} 
İlgili konular