从缓存显示的Android图像显示为锯齿状

时间:2013-12-12 07:37:36

标签: android

我正在下载网络图像并将其保存在应用程序缓存中。下面是下载,保存和显示在屏幕上的代码。

        InputStream is = null;
        URL url1 = new URL(small_img_url);
        HttpURLConnection urlConnection = (HttpURLConnection) url1
                .openConnection();
        urlConnection.setConnectTimeout(12000);
        urlConnection.setReadTimeout(12000);
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();

        is = urlConnection.getInputStream();

        String path = "NEO_IMAGES" + File.separator + "images";
        File file = new File(context.getCacheDir() + File.separator + path
                + File.separator);
        file.mkdirs();
        File file1 = new File(file, fileName + ".jpg");
        if (file1.exists()) {
            file1.delete();
            Log.e("Image already exist", "deleting image=" + fileName);
        }
        // BufferedReader reader = new BufferedReader(new InputStreamReader(
        // is, "UTF-8"));
        FileOutputStream fos = new FileOutputStream(file1);

        Bitmap bitmap = BitmapFactory.decodeStream(is);

        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

        fos.flush();

        fos.close();
        is.close();

                  Uri uriItemIcon = Uri.fromFile(file1);

                  final Bitmap bitmapImage = BitmapFactory.decodeFile(uriItemIcon.getPath());

                  imgItem.setImageBitmap(bitmapImage);

问题是一些不完全呈矩形且有很多曲线的图像看起来是锯齿状的。 请告诉我如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

嘿,你不必这么做。有一些库可以为您缓存。而且他们也很有效率。一个很棒的是Android-Universal-Image-Loader

检查出来。如果你遇到任何问题,你可以问。