Imageloader无法在真实设备上加载图像

时间:2013-08-24 08:24:06

标签: android

我正在使用ImageLoader类从URL加载图像并将其显示在 列表显示。但是这个ImageLoader正在模拟器上加载图像 在真实设备上运行我的应用程序它没有加载任何图像。(只是显示 默认图片)。

请告诉我我必须使用ImageLoader类才能让它在真实设备上运行。

  

ImageLoader类:

public class ImageLoader {

    MemoryCache memoryCache = new MemoryCache();
    FileCache fileCache;
    private Map<ImageView, String> imageViews = Collections
            .synchronizedMap(new WeakHashMap<ImageView, String>());
    ExecutorService executorService;
    // Handler to display images in UI thread
    Handler handler = new Handler();

    public ImageLoader(Context context) {
        fileCache = new FileCache(context);
        executorService = Executors.newFixedThreadPool(5);
    }

    final int stub_id = R.drawable.products;

    public void DisplayImage(String url, ImageView imageView) {
        imageViews.put(imageView, url);
        Bitmap bitmap = memoryCache.get(url);
        if (bitmap != null)
            imageView.setImageBitmap(bitmap);
        else {
            queuePhoto(url, imageView);
            imageView.setImageResource(stub_id);
        }
    }

    private void queuePhoto(String url, ImageView imageView) {
        PhotoToLoad p = new PhotoToLoad(url, imageView);
        executorService.submit(new PhotosLoader(p));
    }

    private Bitmap getBitmap(String url) {
        File f = fileCache.getFile(url);

        Bitmap b = decodeFile(f);
        if (b != null)
            return b;

        // Download Images from the Internet
        try {
            Bitmap bitmap = null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imageUrl
                    .openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is = conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            conn.disconnect();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Throwable ex) {
            ex.printStackTrace();
            if (ex instanceof OutOfMemoryError)
                memoryCache.clear();
            return null;
        }
    }

    // Decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            FileInputStream stream1 = new FileInputStream(f);
            BitmapFactory.decodeStream(stream1, null, o);
            stream1.close();

            // Find the correct scale value. It should be the power of 2.
            // Recommended Size 512
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            FileInputStream stream2 = new FileInputStream(f);
            Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // Task for the queue
    private class PhotoToLoad {
        public String url;
        public ImageView imageView;

        public PhotoToLoad(String u, ImageView i) {
            url = u;
            imageView = i;
        }
    }

    class PhotosLoader implements Runnable {
        PhotoToLoad photoToLoad;

        PhotosLoader(PhotoToLoad photoToLoad) {
            this.photoToLoad = photoToLoad;
        }

        @Override
        public void run() {
            try {
                if (imageViewReused(photoToLoad))
                    return;
                Bitmap bmp = getBitmap(photoToLoad.url);
                memoryCache.put(photoToLoad.url, bmp);
                if (imageViewReused(photoToLoad))
                    return;
                BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
                handler.post(bd);
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }

    boolean imageViewReused(PhotoToLoad photoToLoad) {
        String tag = imageViews.get(photoToLoad.imageView);
        if (tag == null || !tag.equals(photoToLoad.url))
            return true;
        return false;
    }

    // Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable {
        Bitmap bitmap;
        PhotoToLoad photoToLoad;

        public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
            bitmap = b;
            photoToLoad = p;
        }

        public void run() {
            if (imageViewReused(photoToLoad))
                return;
            if (bitmap != null)
                photoToLoad.imageView.setImageBitmap(bitmap);
            else
                photoToLoad.imageView.setImageResource(stub_id);
        }
    }

    public void clearCache() {
        memoryCache.clear();
        fileCache.clear();
    }

}
  

我的GalleryTab类: -

public class GalleryTab extends Fragment {
    GridView gridview;
    ProgressDialog mProgressDialog;
    GridViewAdapter adapter;
    public List<GalleryList> phonearraylist = null;
    View view;
    private WeakReference<RemoteDataTask> asyncTaskWeakRef;

    public static Fragment newInstance(Context context) {
        GalleryTab f = new GalleryTab();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.activity_gallery_tab, null);
        gridview = (GridView) view.findViewById(R.id.gridview);
        setRetainInstance(true);
        startNewAsyncTask();
        // new RemoteDataTask(this).execute();
        return view;
    }

    // RemoteDataTask AsyncTask
    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        private WeakReference<GalleryTab> fragmentWeakRef;

        private RemoteDataTask(GalleryTab gallerytab) {
            this.fragmentWeakRef = new WeakReference<GalleryTab>(gallerytab);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setTitle("Gallery");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array

            phonearraylist = new ArrayList<GalleryList>();
            try {
                for (int i = 0; i <= 6; i++) {
                    GalleryList map = new GalleryList();
                    map.setGallery("http://oi39.tinypic.com/21oydxs.jpg");
                    // System.out.println("PRINT!!!!--  "+ i);
                    phonearraylist.add(map);
                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // if (this.fragmentWeakRef.get() != null) {
            adapter = new GridViewAdapter(getActivity(), phonearraylist);
            // System.out.println("PRINT SIZE --  "+ phonearraylist.size());
            gridview.setAdapter(adapter);

            mProgressDialog.dismiss();
            // }
        }
    }

    private void startNewAsyncTask() {
        RemoteDataTask asyncTask = new RemoteDataTask(this);
        this.asyncTaskWeakRef = new WeakReference<RemoteDataTask>(asyncTask);
        asyncTask.execute();
    }
}
  

我的GridViewAdapter类: -

public class GridViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ImageLoader imageLoader;
    private List<GalleryList> galleryArraylist = null;
    private ArrayList<GalleryList> arraylist;

    public GridViewAdapter(Context context, List<GalleryList> phonearraylist) {
        this.context = context;
        this.galleryArraylist = phonearraylist;
        inflater = LayoutInflater.from(context);
        this.arraylist = new ArrayList<GalleryList>();
        this.arraylist.addAll(phonearraylist);
        imageLoader = new ImageLoader(context);
    }

    public class ViewHolder {
        ImageView phone;
    }

    @Override
    public int getCount() {
        return galleryArraylist.size();
    }

    @Override
    public Object getItem(int position) {
        return galleryArraylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.gridview_item, null);
            // Locate the ImageView in gridview_item.xml
            holder.phone = (ImageView) view.findViewById(R.id.phone);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into ImageView
        imageLoader.DisplayImage(galleryArraylist.get(position).getGallery(),
                holder.phone);
        // Listen for GridView Item Click
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Send single item click data to SingleItemView Class
                Intent intent = new Intent(context, SingleItemView.class);
                // Pass all data phone
                intent.putExtra("gallery",
                        (galleryArraylist.get(position).getGallery()));
                // Start SingleItemView Class
                context.startActivity(intent);
            }
        });
        return view;
    }
}

4 个答案:

答案 0 :(得分:3)

检查清单中的必要权限。

答案 1 :(得分:0)

如果应用程序显示默认图像,则执行以下代码行:

photoToLoad.imageView.setImageBitmap(stub_id);

这意味着您的位图为null。使用debug来找出为什么将null-bitmap传递给BitmapDisplayer的构造函数。

答案 2 :(得分:0)

我可以推荐一种不同于魅力的方式:Android查询。

您可以从这里下载该jar文件

AQuery androidAQuery = new AQuery(this);

举个例子:

androidAQuery.id(你的IMAGEVIEW).image(你的图像加载,true,true,getDeviceWidth(),你想要显示的任何默认图像);

它非常快速和准确,使用它你可以在加载时找到更多功能,如动画;如果需要,获取位图;等

答案 3 :(得分:0)

在清单文件中添加此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />