Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为什么ImageView.setImageURI()在Android 2.2中工作但不在2.1中工作?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用适配器在列表视图显示图像和一些文本.图像从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 one
or more contributor license agreements.  See the NOTICE file
diStributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License,Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless 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 ANY
KIND,either express or implied.  See the License for the
specific language governing permissions and limitations
under 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(); //works
String StringUri = imageUri.toString(); //does not work

i.setImageURI(Uri.parse(StringUri));

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

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。