Inability to compress some deleted images

I have a method that caches images from a remote URL and returns a cached image. If the image is already cached, it simply returns - otherwise it goes to the remote URL and compresses the bitmap into cache.

My problem is that some photos (extracted from Flickr web services) will never be successfully compressand never will be successfully decoded withBitmapFactory.decodeStream(inputStream)

here is my cache method:

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 && 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;
    }

What are the reasons why image.compressit was not possible to save the bitmap?

What are the reasons that BitmapFactory.decoodeStreamraster images will not be created correctly?

It seems that these are just some images (sometimes images that previously could not be downloaded successfully load), which is confusing.

InputStream :

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;
    }

edit: , , G1 (90% ), Samsung Galaxy-s ,

, : g1 galaxy-s, g1. ?

+3

Source: https://habr.com/ru/post/1775228/


All Articles