2013-07-12 36 views
0

嗨,我從url下載圖像&將它們保存在緩存中。然後將這些圖像從緩存加載到輪播視圖中。從緩存中獲取圖像大小android

但問題是當手機分辨率(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; 
    } 

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

+0

你必須更明確地定義你的要求:做你想做增加要下載的圖像的尺寸(例如較大圖像的較大圖像),或者只是增加從同一圖像創建的位圖的尺寸(即縮放圖像以適應屏幕)? – Kai

回答

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