开发者

No such file or directory when caching bitmap?

I keep getting this error

07-28 19:32:40.536: ERROR/error(534): java.io.IOException: No such file or directory
07-28 19:32:40.536: ERROR/error(534):     at java.io.File.createNewFileImpl(Native Method)
07-28 19:32:40.536: ERROR/error(534):     at java.io.File.createNewFile(File.java:1115)
07-28 19:32:40.536: ERROR/error(534):     at com.fttech.gameIT.MainMenu.putBitmapInDiskCache(MainMenu.java:447)

Here is my putBitmapInDiskCache()

    public void putBitmapInDiskCache(URI imageUri, Bitmap avatar) {   

                    File cacheDir = new File(this.getCacheDir(), "thumbnails");    
                    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) {      开发者_如何学Go 
                    Log.e("error", "Error when saving image to cache. ", e);    

                    }  



                    }

EDIT: That worked! Now i get this...

07-28 19:56:31.525: ERROR/DEBUGTAG(573): java.io.FileNotFoundException: /881625833 (No such file or directory)
 07-28 19:56:31.525: ERROR/DEBUGTAG(573):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
  07-28 19:56:31.525: ERROR/DEBUGTAG(573):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
07-28 19:56:31.525: ERROR/DEBUGTAG(573):     at java.io.FileInputStream.<init>(FileInputStream.java:88)
 07-28 19:56:31.525: ERROR/DEBUGTAG(573):     at java.io.FileInputStream.<init>(FileInputStream.java:122)
 07-28 19:56:31.525: ERROR/DEBUGTAG(573):     at com.fttech.gameIT.MainMenu$ImageAdapter.getView(MainMenu.java:297)
 07-28 19:56:31.525: ERROR/DEBUGTAG(573):     at android.widget.Gallery.makeAndAddView(Gallery.java:748)
 07-28 19:56:31.525: ERROR/DEBUGTAG(573):     at android.widget.Gallery.fillToGalleryLeft(Gallery.java:667)

Code i am using to retrieve the images from the cache..

try {


                                URL aURL = new URL(myRemoteImages[position]);



                                URI imageUri = null;
                                try {
                                    imageUri = aURL.toURI();
                                } catch (URISyntaxException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                }
                                if (new File(new File(myContext.getCacheDir(), "thumbnails"), "" + imageUri.hashCode()).exists())
                                {
                                    String cachFile = ""+imageUri.hashCode();
                                    FileInputStream fis;

                                    try {
                                        fis = new FileInputStream(cachFile);
                                        Bitmap bm = BitmapFactory.decodeStream(fis); 
                                         i.setImageBitmap(bm);
                                            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                                            /* Set the Width/Height of the ImageView. */
                                            if(Build.VERSION.SDK_INT >= 11){
                                                i.setLayoutParams(new Gallery.LayoutParams(450, 300));
                                            }
                                            else{
                                                i.setLayoutParams(new Gallery.LayoutParams(125, 125));
                                            }


                                    } catch (FileNotFoundException e) {

                                           Log.e("DEBUGTAG", "Remtoe Image Exception", e);





                                            /* Image should be scaled as width/height are set. */
                                            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                                            /* Set the Width/Height of the ImageView. */
                                            if(Build.VERSION.SDK_INT >= 11){
                                                i.setLayoutParams(new Gallery.LayoutParams(450, 300));

                                            return i;
                                            }
                                                i.setLayoutParams(new Gallery.LayoutParams(125, 125));
                                                return i;
                                            }


You are missing a cacheDir.mkdirs() call. Do that and then do File cacheFile...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜