2013-07-12 5 views
0

Привет Я загружаю изображения с url & сохранять их в кеше. Затем загрузите эти изображения из кеша в виде карусели.Увеличить размеры изображения получить от кеша android

но проблема в таком случае разрешение телефона (720X1124) большой размер изображения стать маленький.

Привожу код изображения сохранить & показать им ОКН ...

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; 
    } 
Смежные вопросы