2

我正在使用 ImageLoader 类从 url 加载图像并将其显示在列表视图中。但是这个 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

4 回答 4

3

检查清单中的必要权限。

于 2013-08-24T08:40:40.517 回答
0

在清单文件中添加此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-11-16T08:09:15.370 回答
0

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

photoToLoad.imageView.setImageBitmap(stub_id);

这意味着您的位图是null. 使用调试找出为什么将 null-bitmap 传递给BitmapDisplayer.

于 2013-08-24T08:45:02.250 回答
0

我可以推荐一种像魅力一样工作的不同方式:Android Query。

您可以从此处下载该 jar 文件

AQuery androidAQuery=new AQuery(this);

举个例子:

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

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

于 2013-08-24T10:02:23.043 回答