为什么ImageView.setImageURI()在Android 2.2中工作但不在2.1中工作?

为什么ImageView.setImageURI()在Android 2.2中工作但不在2.1中工作?,第1张

概述我正在使用适配器在列表视图中显示图像和一些文本.图像从Web中提取,然后在本地缓存并显示.图像已经很小(60px方形),我知道它们的大小,所以我使用的是 advice from here suggesting I use setImageURI instead of decoding the bitmap. 完成这项工作的课程是Fedor ImageLoader的修改版本 代码将一个可绘制的存根附 我正在使用适配器在列表视图中显示图像和一些文本.图像从Web中提取,然后在本地缓存并显示.图像已经很小(60px方形),我知道它们的大小,所以我使用的是 advice from here suggesting I use setImageURI instead of decoding the bitmap.

完成这项工作的课程是Fedor ImageLoader的修改版本

代码将一个可绘制的存根附加到ImageVIEw,直到从Web下载所需的图像,然后从SD卡加载缓存的文件.在AndroID 2.2中,这很好用.这很快,我没有OOM崩溃.但是在2.1上,我收到以下错误:

09-15 11:04:52.993: INFO/System.out(240): resolveUri Failed on bad bitmap uri: file:///sdcard/androID/data/com.example.myapp/cache/4164137

ImageLoader类如下:

/*licensed to the Apache Software Foundation (ASF) under oneor more contributor license agreements.  See the NOTICE filedistributed with this work for additional informationregarding copyright ownership.  The ASF licenses this fileto you under the Apache license,Version 2.0 (the"license"); you may not use this file except in compliancewith the license.  You may obtain a copy of the license athttp://www.apache.org/licenses/liCENSE-2.0Unless required by applicable law or agreed to in writing,software distributed under the license is distributed on an"AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND,either express or implIEd.  See the license for thespecific language governing permissions and limitationsunder the license.    */public class ImageLoader {    //the simplest in-memory cache implementation. This should be replaced with something like SoftReference or BitmapOptions.inPurgeable(since 1.6)    private HashMap<String,Uri> cache=new HashMap<String,Uri>();    private file cacheDir;    public ImageLoader(Context context){        //Make the background thead low priority. This way it will not affect the UI performance        photoloaderThread.setPriority(Thread.norM_PRIORITY-1);        //Find the dir to save cached images        if (androID.os.Environment.getExternalStorageState().equals(androID.os.Environment.MEDIA_MOUNTED))            cacheDir=new file(androID.os.Environment.getExternalStorageDirectory(),"AndroID/data/com.droIDicon.launcherproicons/cache/");        else            cacheDir=context.getCacheDir();        if(!cacheDir.exists())            cacheDir.mkdirs();    }    final int stub_ID=R.drawable.loading;    public voID displayImage(String url,Activity activity,ImageVIEw imageVIEw,int scaleSize)    {        if(cache.containsKey(url))            imageVIEw.setimageURI(cache.get(url));        else        {            queuePhoto(url,activity,imageVIEw,scaleSize);            imageVIEw.setimageResource(stub_ID);        }        }    private voID queuePhoto(String url,int scaleSize)    {        //This ImageVIEw may be used for other images before. So there may be some old tasks in the queue. We need to discard them.         photosQueue.Clean(imageVIEw);        PhotoToload p=new PhotoToload(url,scaleSize);        synchronized(photosQueue.photosToload){            photosQueue.photosToload.push(p);            photosQueue.photosToload.notifyAll();        }        //start thread if it's not started yet        if(photoloaderThread.getState()==Thread.State.NEW)            photoloaderThread.start();    }    private Uri getUri(String url,int scaleSize){        if(url != ""){            //I IDentify images by hashcode. Not a perfect solution,good for the demo.//          try{            String filename=String.valueOf(url.hashCode());            file f=new file(cacheDir,filename);            //from SD cache            if(f.exists()){                Uri b = Uri.fromfile(f);                System.out.println(f.toString());                return b;            }            //from web            try {                Uri bitmap=null;                inputStream is=new URL(url).openStream();                OutputStream os = new fileOutputStream(f);                Utils.copyStream(is,os);                os.close();                bitmap = Uri.fromfile(f);                System.out.println(f.toString());                return bitmap;            } catch (Exception ex){               ex.printstacktrace();               return null;            }            } else {                return null;            }    }    //Task for the queue    private class PhotoToload    {        public String url;        public ImageVIEw imageVIEw;        public int scaleSize;        public PhotoToload(String u,ImageVIEw i,int ss){            url=u;             imageVIEw=i;            scaleSize=ss;        }    }    PhotosQueue photosQueue=new PhotosQueue();    public voID stopThread()    {        photoloaderThread.interrupt();    }    //stores List of photos to download    class PhotosQueue    {        private Stack<PhotoToload> photosToload=new Stack<PhotoToload>();        //removes all instances of this ImageVIEw        public voID Clean(ImageVIEw image)        {            for(int j=0 ;j<photosToload.size();){                if(photosToload.get(j).imageVIEw==image)                    photosToload.remove(j);                else                    ++j;            }        }    }    class Photosloader extends Thread {        public voID run() {            try {                while(true)                {                    //thread waits until there are any images to load in the queue                    if(photosQueue.photosToload.size()==0)                        synchronized(photosQueue.photosToload){                            photosQueue.photosToload.wait();                        }                    if(photosQueue.photosToload.size()!=0)                    {                        PhotoToload photoToload;                        synchronized(photosQueue.photosToload){                            photoToload=photosQueue.photosToload.pop();                        }                        Uri bmp=getUri(photoToload.url,photoToload.scaleSize);                        cache.put(photoToload.url,bmp);                        if(((String)photoToload.imageVIEw.getTag()).equals(photoToload.url)){                            Uridisplayer bd=new Uridisplayer(bmp,photoToload.imageVIEw);                            Activity a=(Activity)photoToload.imageVIEw.getContext();                            a.runOnUiThread(bd);                        }                    }                    if(Thread.interrupted())                        break;                }            } catch (InterruptedException e) {                //allow thread to exit            }        }    }    Photosloader photoloaderThread=new Photosloader();    class Uridisplayer implements Runnable    {        Uri uri;        ImageVIEw imageVIEw;        public Uridisplayer(Uri u,ImageVIEw i){uri=u;imageVIEw=i;}        public voID run() {            file f = new file(uri.getPath());            if(f.exists()){                imageVIEw.setimageURI(uri);            } else {                imageVIEw.setimageResource(stub_ID);            }        }    }    public voID clearCache() {        //clear memory cache        cache.clear();        //clear SD cache        file[] files=cacheDir.Listfiles();        for(file f:files)            f.delete();    }}

以下是实现此ImageLoader的适配器之一:

public class colorAdapter extends ArrayAdapter<color> {    private Activity activity;    private int resource;    private String response;    private Context context;    public ImageLoader imageLoader;    public colorAdapter(Activity a,Context context,int resource,List<color> items){        super(context,resource,items);        this.resource=resource;        imageLoader=new ImageLoader(context);        activity = a;    }    @OverrIDe    public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent){        color color =  getItem(position);        String minflater = Context.LAYOUT_INFLATER_SERVICE;        LayoutInflater inflater;        inflater = (LayoutInflater)getContext().getSystemService(minflater);        VIEwHolder holder;        if(convertVIEw==null){            convertVIEw = inflater.inflate(R.layout.Listcolors,parent,false);            holder=new VIEwHolder();            holder.txtname=(TextVIEw)convertVIEw.findVIEwByID(R.ID.txtname);            //holder.txtUser=(TextVIEw)convertVIEw.findVIEwByID(R.ID.txtUser);            holder.imgcolorimg=(ImageVIEw)convertVIEw.findVIEwByID(R.ID.imgcolorimg);            convertVIEw.setTag(holder);        }        else            holder=(VIEwHolder)convertVIEw.getTag();        holder.txtname.setText(color.getname());        //holder.txtUser.setText(dock.getUser());        holder.imgcolorimg.setTag(color.getIconURL());        imageLoader.displayImage(color.getIconURL(),holder.imgcolorimg,84);        return convertVIEw;    }    class VIEwHolder{        TextVIEw txtname;        //TextVIEw txtUser;        ImageVIEw imgcolorimg;    }}
解决方法 我有同样的问题,事实证明,2.1需要将Uri保存为字符串为uriVaribale.getPath()…示例:
String stringUri = imageUri.getPath(); //worksString stringUri = imageUri.toString(); //does not worki.setimageURI(Uri.parse(stringUri));

当使用getPath()时,它似乎适用于所有AndroID *** 作系统.

总结

以上是内存溢出为你收集整理的为什么ImageView.setImageURI()在Android 2.2中工作但不在2.1中工作?全部内容,希望文章能够帮你解决为什么ImageView.setImageURI()在Android 2.2中工作但不在2.1中工作?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1143708.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存