Failing to compress SOME remote images
<p>I have a method which caches images from a remote URL and returns the cached image.  If the image is already cached, it is simply returned - otherwise it goes to the remote URL and compresses the bitmap to the cache.</p>

<p>My problem is that some photos(fetched from Flickr's web services), never successfully  <code>compress</code>, and never successfully get decoded via <code>BitmapFactory.decodeStream(inputStream)</code></p>

<p>here is my cache method:</p>

<pre><code>public static Bitmap createImage(Context context, String imageUrl, String imageName) {
        Bitmap image = null;
        try {
            if(new File(context.getCacheDir(), imageName).exists())
                return BitmapFactory.decodeFile(new File(context.getCacheDir(), imageName).getPath());

            //load file from web
            image = BitmapFactory.decodeStream(HttpClient.fetchInputStream(imageUrl));

            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(new File(context.getCacheDir(), imageName));
            }

            //this should never happen
            catch(FileNotFoundException e) {
                if(Constants.LOGGING)
                    Log.e(TAG, e.toString(), e);
            }

            //if the file couldn't be saved
            if(fos != null &amp;&amp; image != null) {
                if(!image.compress(Constants.BITMAP_CACHE_COMPRESS_FORMAT, Constants.CACHE_IMAGE_QUALITY, fos)) {
                    if(Constants.LOGGING)
                        Log.e(TAG, "The image could not be saved: " + imageName + " - " + imageUrl);
                    image = BitmapFactory.decodeResource(context.getResources(), R.drawable.twitter_default_icon);
                }
                fos.flush();
                fos.close();
            }
            else
                image = BitmapFactory.decodeResource(context.getResources(), R.drawable.twitter_default_icon);
        }
        catch(IOException e) {
            if(Constants.LOGGING)
                Log.e(TAG, e.toString(), e);
        }
        return image;
    }
</code></pre>

<p>What are the reasons that <code>image.compress</code> would not successfully save the bitmap?</p>

<p>What are the reasons that <code>BitmapFactory.decoodeStream</code> would not properly create a bitmap?</p>

<p>It only seems to be some images(sometimes images that couldn't load before, do successfully load), which is confusing.</p>

<p>Here is how i get the <code>InputStream</code> of a remote image:</p>

<pre><code>protected static InputStream fetchInputStream(String url) {
        try {
            return new URL(url.replace(" ", "%20")).openStream();
        }
        catch(MalformedURLException e) {
            if(Constants.LOGGING)
                Log.e(TAG, e.toString());
        }
        catch(IOException e) {
            if(Constants.LOGGING)
                Log.e(TAG, e.toString());
        }
        return null;
    }
</code></pre>

<p>edit: i don't seem to be having trouble on the G1(90% of the time), but on the Samsung Galaxy-s i am having many problems showing many images</p>

<p>editing my edit, erg: some sets of images work perfectly on the g1 AND galaxy-s, and some sets of images mostly only work on the g1. ugh what the crap is going on?</p>
