2010-12-11 23 views
8

मैं सर्वर से एक दूरस्थ छवि लोड करने का प्रयास करता हूं और स्टैक ओवरफ्लो पर कई कोड उदाहरणों के लिए धन्यवाद, मेरे पास एक समाधान है जो 3 में से 2 छवियों में काम करता है। मुझे वास्तव में पता नहीं है कि तीसरी तस्वीर के साथ समस्या क्या है और कभी-कभी डीबगर में कोड चलाने के दौरान तस्वीर लोड हो रही है। अगर मैं समस्या की तस्वीर को पहले लोड करता हूं तो पहले दो अन्य चित्र कभी-कभी लोड नहीं होते हैं।BitmapFactory.decodeStream अपवाद के बिना शून्य लौटाता है

यहाँ कोड है:

public static Drawable getPictureFromURL(Context ctx, String url, final int REQUIRED_SIZE) throws NullPointerException { 
    //Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    int scale = 1; 
    if (o.outWidth > REQUIRED_SIZE) { 
     scale = (int) Math.pow(2, (int) Math.round(Math.log(REQUIRED_SIZE/(double) Math.max(o.outHeight, o.outWidth))/Math.log(0.5))); 
    } 
    Log.i(Prototype.TAG, "scale: "+scale); 

    //Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    Bitmap bmp; 
    try { 
     bmp = BitmapFactory.decodeStream((InputStream) Tools.fetch(url), null, o2); 
     if(bmp!=null) 
      return new BitmapDrawable(ctx.getResources(), bmp); 
     else 
      return null; 
    } catch (Exception e) { 
     Log.e(Prototype.TAG, "Exception while decoding stream", e); 
     return null; 
    } 
} 

मुझे पता चला कि o.outWidth है -1 जो ​​एक त्रुटि दर्शाता है, लेकिन कोई अपवाद फेंक दिया है डिबगिंग के दौरान, इसलिए मैं वास्तव में नहीं बता सकता कि गलत हो गया। इनपुटस्ट्रीम हमेशा एक वैध मान लौटाता है, और मुझे पता है कि तस्वीर सर्वर पर मौजूद है।

शुभकामनाएं, डैनियल

उत्तर

16

मैं जवाब here पाया जाता है और करने के लिए विधि लाने अद्यतन:

private static InputStream fetch(String address) throws MalformedURLException,IOException { 
    HttpGet httpRequest = new HttpGet(URI.create(address)); 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); 
    HttpEntity entity = response.getEntity(); 
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
    InputStream instream = bufHttpEntity.getContent(); 
    return instream; 
} 
+0

+1, लेकिन यह है कि छवियों के अनुक्रम में के बजाय समानांतर रूप से डाउनलोड किया जा रहा है अब लगता है (मैं एक अलग AsyncTask पर प्रत्येक छवि dl-ing हूँ)। कम से कम यह काम करता है .. – kellogs