2011-01-01 13 views
8

मैंने छवि को यूआरएल से लाने के लिए नीचे दिए गए कोड का उपयोग किया लेकिन यह बड़ी छवियों के लिए काम नहीं कर रहा है।मैं एक यूआरएल से बड़ी छवि कैसे प्राप्त कर सकता हूं?

क्या मुझे उस प्रकार की छवि लाने पर कुछ याद आ रहा है?

imgView = (ImageView)findViewById(R.id.ImageView01); 
    imgView.setImageBitmap(loadBitmap("http://www.360technosoft.com/mx4.jpg")); 

    //imgView.setImageBitmap(loadBitmap("http://sugardaddydiaries.com/wp-content/uploads/2010/12/how_do_i_get_sugar_daddy.jpg")); 
    //setImageDrawable("http://sugardaddydiaries.com/wp-content/uploads/2010/12/holding-money-copy.jpg"); 
    //Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png"); 
    //imgView.setImageDrawable(drawable); 

/* try { 
    ImageView i = (ImageView)findViewById(R.id.ImageView01); 
    Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://sugardaddydiaries.com/wp-content/uploads/2010/12/holding-money-copy.jpg").getContent()); 
    i.setImageBitmap(bitmap); 
    } catch (MalformedURLException e) { 
    System.out.println("hello"); 
    } catch (IOException e) { 
    System.out.println("hello"); 
    }*/ 
    } 


protected Drawable ImageOperations(Context context, String string, 
    String string2) { 
    // TODO Auto-generated method stub 
    try { 
    InputStream is = (InputStream) this.fetch(string); 
    Drawable d = Drawable.createFromStream(is, "src"); 
    return d; 
    } catch (MalformedURLException e) { 
    e.printStackTrace(); 
    return null; 
    } catch (IOException e) { 
    e.printStackTrace(); 
    return null; 
    } 

} 
+0

मुझे यकीन है कि 'loadBitmap' स्पष्ट रूप से फ़ाइल डाउनलोड करता है ... धागे का उपयोग किए बिना, क्या मैं सही हूँ? – st0le

उत्तर

5

आप .... यह

उपयोग AsyncTask छवियों डाउनलोड करने के लिए, कि जिस तरह से एक ANR (अनुप्रयोग जवाब देते नहीं) का कारण होगा यूआई थ्रेड के भीतर से बड़ी छवि डाउनलोड करने के लिए कोशिश कर रहे हैं, एक पृथक धागे का उपयोग डाउनलोड के लिए किया जाएगा और आपका यूआई थ्रेड लॉक नहीं होगा।

+0

st0le सही है। एक AsyncTask का उपयोग करें ... उदाहरण के लिए: http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html – RyanM

0
 public class MyImageActivity extends Activity 
    { 
     private String image_URL= "http://home.austarnet.com.au/~caroline/Slideshows/Butterfly_Bitmap_Download/slbutfly.bmp"; 

     private ProgressDialog pd; 
     private Bitmap bitmap; 
     private ImageView bmImage; 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 

       bmImage = (ImageView)findViewById(R.id.imageview); 
       pd = ProgressDialog.show(this, "Please Wait", "Downloading Image"); 
       DownloadWebPageTask task = new DownloadWebPageTask(); 
       task.execute(new String[] { image_URL }); 
       // You can also give more images in string array 

     } 
     private class DownloadWebPageTask extends AsyncTask<String, Void, Bitmap> 
     { 
      // String --> parameter in execute 
      // Bitmap --> return type of doInBackground and parameter of onPostExecute 
      @Override 
      protected Bitmap doInBackground(String...urls) { 
       String response = ""; 
       for (String url : urls) 
       { 
        InputStream i = null; 
        BufferedInputStream bis = null; 
        ByteArrayOutputStream out =null; 

//    Only for Drawable Image   
//    try 
//    { 
//     URL url = new URL(image_URL); 
//     InputStream is = url.openStream(); 
//     Drawable d = Drawable.createFromStream(is, "kk.jpg"); 
//     bmImage.setImageDrawable(d); 
//    } 
//   catch (Exception e) { 
//    // TODO: handle exception 
//   } 

// THE ABOVE CODE IN COMMENTS WILL NOT WORK FOR BITMAP IMAGE      

try 
        { 
         URL m = new URL(image_URL); 
         i = (InputStream) m.getContent(); 
         bis = new BufferedInputStream(i, 1024*8); 
         out = new ByteArrayOutputStream(); 
         int len=0; 
         byte[] buffer = new byte[4096]; 
         while((len = bis.read(buffer)) != -1) 
         { 
          out.write(buffer, 0, len); 
         } 
         out.close(); 
         bis.close(); 

        byte[] data = out.toByteArray(); 
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 
        } 
        catch (Exception e) 
        { 
         e.printStackTrace(); 
        } 
        } 

       return bitmap; 
      } 

      @Override 
      protected void onPostExecute(Bitmap result) 
      { 
       if(result!=null) 
       bmImage.setImageBitmap(result); 
       pd.dismiss(); 
      } 
     } 
    } 
2

आप तो छवि डाउनलोड करने के लिए बहुत जल्दी चाहते हैं तो आप AQuery जो JQuery के समान है सिर्फ android-query.0.15.7.jar

आयात जार फ़ाइल डाउनलोड करने और निम्नलिखित स्निपेट

जोड़ने का उपयोग कर सकते
AQuery aq = new AQuery(this); 
aq.id(R.id.image).image("http://4.bp.blogspot.com/_Q95xppgGoeo/TJzGNaeO8zI/AAAAAAAAADE/kez1bBRmQTk/s1600/Sri-Ram.jpg"); 

// Here R.id.image is id of your ImageView 
// This method will not give any exception 
// Dont forgot to add Internet Permission 
+0

AQuery उत्कृष्ट है, भले ही आप कभी भी async के लिए उपयोग करते हैं छवि को जांचने के लायक है। – scottyab

संबंधित मुद्दे