开发者

How to scale Bitmap put, and loaded from cache initially?

This is a method i created to put Bitmap into cache..

        //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();       
            FileOutputStrea开发者_StackOverflow社区m 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);    

            }   



            }

The only problem is i set the Bitmap width and height parameter initially when it is downloaded. But when it is put into cache and pulled out, it loses its width and height and becomes to big. When it is scrolled off the screen it is set correctly again. intially it loses its width and height.

EDIT: I load the bitmap with my getimagefromCache() method.

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


There is an Matrix parameter on the createBitmap method that will allow you to resize your Bitmap and so you can store it with the resolution you want. See how here (it's an excellent tutorial, by the way).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜