从缓存android增加图像大小

时间:2013-07-12 10:11:01

标签: android performance bitmap

您好我从url&中下载图片将它们保存在缓存中。然后将这些图像从缓存加载到轮播视图中。

但问题是手机分辨率(720X1124)高时图像尺寸变小。

这里我给出了图片代码save&从卡车中展示出来......

 private Bitmap getBitmap(String url) 
    {
        File f=fileCache.getFile(url);


        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)
            return b;

        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(300000000);
            conn.setReadTimeout(300000000);
            conn.setInstanceFollowRedirects(true);
            InputStream inputstream=conn.getInputStream();
            OutputStream outputstream = new FileOutputStream(f);
            Utils.CopyStream(inputstream, outputstream);
            outputstream.close();
            conn.disconnect();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Throwable ex){
           ex.printStackTrace();
           if(ex instanceof OutOfMemoryError)
               memoryCache.clear();
           return null;
        }
    }

   public void getDimension(int width,int height){
       widthScreen=width;
       heightScreen=height;

   }
    //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            final int IMAGE_MAX_SIZE = 120000000; // 1.2MP

            BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
            scaleOptions.inJustDecodeBounds = true;
            FileInputStream stream1=new FileInputStream(f);
            BitmapFactory.decodeStream(stream1,null,scaleOptions);
            stream1.close();

         // find the correct scale value as a power of 2.
            int scale = 1;
            while (scaleOptions.outWidth / scale / 2 >= widthScreen
              && scaleOptions.outHeight / scale / 2 >= heightScreen) {
              scale *= 2;
            }


           Bitmap bitmap = null;
            if (scale > 1) {
               scale--;
                // scale to max possible inSampleSize that still yields an image
                // larger than target
               scaleOptions = new BitmapFactory.Options();
               scaleOptions.inSampleSize = scale;
                bitmap = BitmapFactory.decodeStream(stream1, null, scaleOptions);


                int width=widthScreen;
                int height=heightScreen;

                double y=height;
                double x=width;


                Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, (int) x, 
                   (int) y, true);
                bitmap.recycle();
                bitmap = scaledBitmap;

                System.gc();
            } else {
                bitmap = BitmapFactory.decodeStream(stream1);
            }
            if((widthScreen>=450)&& (heightScreen>=750) ) {
                sample=1;
            }
            else{
                sample=2;
            }
            //decode with inSampleSize
            BitmapFactory.Options scalOption = new BitmapFactory.Options();
            scalOption.inSampleSize=sample;
            FileInputStream stream2=new FileInputStream(f);
            Bitmap bitMap=BitmapFactory.decodeStream(stream2, null, scalOption);
            stream2.close();
            return bitMap;
        } catch (FileNotFoundException e) {
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

如何根据手机分辨率增加图像尺寸。我已经厌倦了更多的日子来克服这个问题。但它不起作用。所以给我正确的指导....... 感谢............

3 个答案:

答案 0 :(得分:1)

您需要为不同的屏幕尺寸/分辨率定义布局。 请参阅Supporting Multiple Screens

答案 1 :(得分:1)

尝试使用此image loader library

加载图片

答案 2 :(得分:0)

public int  calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;


        int inSampleSize = 1;
        if (height < reqHeight || width < reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }

将此方法放入我的代码中,并按以下方式调用此方法,

private Bitmap decodeFile(File f) {
        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            FileInputStream stream1 = new FileInputStream(f);
            BitmapFactory.decodeStream(stream1, null, o);
            stream1.close();


             Matrix matrix = new Matrix();
             // matrix.postScale(scaleWidth, scaleHeight);
              matrix.postRotate(45);
            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            //calculateInSampleSize1(o, widthScreen,heightScreen);

            o2.inSampleSize=calculateInSampleSize(o, widthScreen,heightScreen);;
            FileInputStream stream2 = new FileInputStream(f);
            o2.inJustDecodeBounds = false;
            Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);

            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }