簡體   English   中英

從緩存android增加圖像大小

[英]Increase image sizes get from cache android

嗨我從url下載圖像並將其保存在緩存中。 然后將這些圖像從緩存加載到輪播視圖中。

但問題是當手機分辨率(720X1124)高時圖像尺寸變小。

在這里,我給出了圖像代碼保存並從cach中顯示出來......

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

如何根據手機分辨率增加圖像尺寸。 我已經厭倦了更多的日子來克服這個問題。 但它不起作用。 所以給我正確的指導.......謝謝............

您需要為不同的屏幕尺寸/分辨率定義布局。 請參閱支持多個屏幕

嘗試使用此圖像加載器庫加載圖像

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM