2011-08-25 4 views
0

이 난 캐시에 비트 맵을 넣어 만든 방법 ..처음 비트 맵을 넣고 캐시에서로드하는 방법은 무엇입니까?

 //Caching method to cache images loaded from the URL's. 
    public void putBitmapInDiskCache(URI imageUri, Bitmap avatar) { 
      File cacheDir = new File(MainMenu.this.getCacheDir(), "thumbnails"); 
      cacheDir.mkdirs(); 
      File cacheFile = new File(cacheDir, ""+imageUri.hashCode()); 
      try {  
      cacheFile.createNewFile();  
      FileOutputStream fos = new FileOutputStream(cacheFile);  
      avatar.compress(CompressFormat.PNG, 100, fos); 


      fos.flush();  
      fos.close();  
      } catch (Exception e) {  
      Log.e("error", "Error when saving image to cache. ", e);  

      } 



      } 

유일한 문제는이 다운로드 될 때 초기 나 비트 맵 폭 및 높이 파라미터 설정되어있다. 그러나 캐시에 넣고 당기면 너비와 높이가 줄어들고 커지게됩니다. 화면에서 스크롤하면 올바르게 다시 설정됩니다. intially 그것의 너비와 높이를 잃는다.

편집 : 내 getimagefromCache() 메서드를 사용하여 비트 맵을로드합니다.

public void getImagesfromCache() throws MalformedURLException{ 
if(new File(new File(this.getApplicationContext().getCacheDir(), "thumbnails"),"" + imageCacheUri.hashCode()).exists()){ 
String cacheFile = this.getApplicationContext().getCacheDir() +"/thumbnails/"+ imageCacheUri.hashCode(); 
      ImageView i = new ImageView(this.getApplicationContext()); 
      FileInputStream fis = null; 
      try { 
       fis = new FileInputStream(cacheFile); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Bitmap bm = BitmapFactory.decodeStream(fis); 

      i.setImageBitmap(bm); 
      i.setTag(mImageURLs); 
      putBitmapInDiskCache(imageCacheUri, bm); 



     //Set the BaseAdapter to a gallery, holding the images 
      ((Gallery) findViewById(R.id.gallery)) 
       .setAdapter(new ImageAdapter(MainMenu.this)); 
+0

어떻게 비트 맵 이미지를로드합니까? 코드를 보면 더 좋아질 것입니다 – Ronnie

+0

내 편집을 확인하십시오 – yoshi24

+0

get에 putBitmapInDiskCache를 호출하는 이유는 무엇입니까? – Ronnie

답변

2

사용하면 비트 맵의 ​​크기를 조정 할 수 있습니다 그래서 당신은 당신이 원하는 해상도로 저장할 수있는 createBitmap 방법에 Matrix 매개 변수가있다. 보기 here (훌륭한 튜토리얼입니다.).

+0

내 상황과 관련된 코드를 제공해 주시겠습니까? – yoshi24

+0

내 게시물에 링크를 삽입 했습니까? 거기에서 내가 말한 것과 관련된 코드를 찾을 수 있습니다. 문제는 비트 맵의 ​​크기를 적절하게 조절할 수 있으므로 원하는 해상도로 유지할 수 있다는 것입니다. 시도해 볼 가치가 있습니다. 링크 (다시) : http://www.anddev.org/resize_and_rotate_image_-_example-t621.html – Alesqui

관련 문제