5

हाय दोस्तों, अच्छी तरह से मैं आईडी नंबर के अनुसार सर्वर से छवियों को लोड करने की कोशिश कर रहा हूं, तो यह webservice से इसे पुनः प्राप्त करने का सबसे अच्छा तरीका होगा क्या कोई मुझे मार्गदर्शन कर सकता है? ?वेब सेवा से एंड्रॉइड एप्लिकेशन पर छवि कैसे प्राप्त करें

package com.crispsys; 

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class imageviewer extends Activity { 
    /** Called when the activity is first created. */ 

    Bitmap image; 
    private TextView ordernumber; 
    ImageView imageView; 
    private Button butt; 
    String Str="image",ordno; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.imageviewerr); 

     this.butt = (Button) this.findViewById(R.id.getbutt); 
      this.imageView =(ImageView) findViewById(R.id.blodig); 
      this.ordernumber=(TextView)this.findViewById(R.id.ordernotext); 

      final Bundle getdata = getIntent().getExtras(); 

      if (getdata != null)  
      { 
       ordno=getdata.getString("orono"); 
       ordernumber.setText(ordno); 
      } 


     this.butt.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 


       Toast.makeText(getApplicationContext(),"loading" , Toast.LENGTH_LONG).show(); 

         URL url = null; 


         try 
         { 

          url = new URL("http://www.cyberstudents.in/aaa/car.jpg"); 

         } 
         catch (MalformedURLException e) 
         { 
          e.printStackTrace(); 
         } 
         try { 
          image = BitmapFactory.decodeStream(url.openStream()); 

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

       imageView.setImageBitmap(image); 
      } 
     }); 

    } 
} 

एक्सएमएल फ़ाइल:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:background="@drawable/cr_isp" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:paddingTop="20dp" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/ordernotext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#8B8682" 
      android:text="" /> 


    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:paddingTop="20dp" 
     android:layout_height="wrap_content" > 

     <Button 
      android:id="@+id/getbutt" 
      android:layout_width="210dp" 
      android:layout_height="40dp" 
      android:background="@drawable/bcbut" 
      android:textColor="#8B8682" 
      android:text="Get Block Dig" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:paddingTop="20dp" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/blodig" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_launcher" /> 

    </TableRow> 

</LinearLayout> 
  • इस कोडिंग के साथ की कोशिश की, लेकिन मैं केवल छोटे आकार छवियों के साथ काम कर सकता था जब मैं बड़ा आकार छवियों यह काम करता है नहीं करता है के लिए जाना है, यह एक ही रहता है, मैं नहीं कर सकते किसी भी त्रुटि को एक ही स्क्रीन दिखाते हुए
+0

अपने प्रश्न का वर्णन करें। क्या आप एक्सएमएल पार्स कर रहे हैं ?? –

+0

एंड्रॉइड में यूनिवर्सल इमेज लोडर को खोजने का प्रयास करें –

+0

कृष्णा सुथर एक्टुल्ली मैंने दोनों प्रकार के यूआरएल के साथ कोशिश की और बेस 64 कनवर्शन स्ट्रिंग में भी जब मैं उपरोक्त कोड के साथ जांच करता हूं तो मैं छोटे आकार की छवियों को लोड करने में सक्षम हो सकता हूं, – user1389233

उत्तर

1

उस आईडी नंबर के साथ webservice पर अनुरोध प्राप्त करें और सेवा आवश्यक छवि के साथ प्रतिक्रिया दे।

2

यह सर्वर से प्रतिक्रिया पर निर्भर करता है, यानी यह किस रूप में यह छवि प्रदान कर रहा है। यह बेस 64 स्ट्रिंग या यूआरएल के रूप में हो सकता है।

यदि यह आधार 64 स्ट्रिंग है तो आप इसे जावा में बेस 64 कक्षा का उपयोग करके वापस एन्कोड करने के लिए और यदि उसका यूआरएल है तो आपको छवि को बिटमैप का उपयोग करके बिटमैप का उपयोग करके छवि को सेट करने की आवश्यकता है। आपको httpGet का उपयोग कर सर्वर पर अनुरोध करने की आवश्यकता है।

public static String connect(String url) throws IOException { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet(url); 
    HttpResponse response; 
    try { 
     response = httpclient.execute(httpget); 
     HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream instream = entity.getContent(); 
      result = convertStreamToString(instream); 
      instream.close(); 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return result; 
} 


private static String convertStreamToString(InputStream input) { 
    /* 
    * To convert the InputStream to String we use the 
    * BufferedReader.readLine() method. We iterate until the BufferedReader 
    * return null which means there's no more data to read. Each line will 
    * appended to a StringBuilder and returned as String. 
    */ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 
    StringBuilder buffer = new StringBuilder(); 
    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      buffer.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      input.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return buffer.toString(); 
} 
+0

क्यू छवियों के बारे में नहीं है । – Tina

10

आप छवियों को असीमित रूप से लोड करने के लिए असीमित छवि लोडर का उपयोग कर सकते हैं। नीचे कोड की जांच करें।

private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> { 
    @Override 
    protected Bitmap doInBackground(String... arg0) { 
     Bitmap b = null; 
     try { 
      b = BitmapFactory.decodeStream((InputStream) new URL(arg0[0]).getContent()); 
     } 
     catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return b; 
    } 
} 

इस कोड का उपयोग लागू करने के लिए।

new FetchImageTask() { 
     @Override 
     protected void onPostExecute(Bitmap result) { 
      if (result != null) { 
       image.setImageBitmap(result); 
      } 
     } 
    }.execute("IMAGE_URL"); 
संबंधित मुद्दे