0

URLから画像を読み込んでAndroidギャラリーウィジェットに表示したい。

だから私は onCreate() メソッドで以下のコードを書きました。

        list = GuideDAO.getAllImages(businessId);


        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setSpacing(2);

        // Set the adapter to our custom adapter (below)
        if(list.size() > 0)
        {
            g.setAdapter(new ImageAdapter(this,list));
        }

これは私のImageAdapterです

public class ImageAdapter extends BaseAdapter {
       List<Images> glist = null;
       private String url;
        public ImageAdapter(Context c,List<Images> lst) {
            mContext = c;
            glist = lst;
            int i=0;
            for (Images id : glist) {
                 url = id.getImageURL(); // Getting URL
                 InputStream inStream = null;
                    if (url.startsWith("http")) {

                        url = url.replace(" ", "%20");
                        HttpURLConnection conn;
                        try {
                             conn = (HttpURLConnection)new URL(url).openConnection();
                             conn.setDoInput(true);
                             conn.connect();
                             inStream = conn.getInputStream();
                        } catch (MalformedURLException e) {

                            e.printStackTrace();
                        } catch (IOException e) {

                            e.printStackTrace();
                        }

                    } else {
                        try {
                            inStream = new FileInputStream(url);
                        } catch (FileNotFoundException e) {

                            e.printStackTrace();
                        }
                    }

                     BitmapFactory.Options options = new BitmapFactory.Options();
                     options.inPreferredConfig = Bitmap.Config.RGB_565;
                     options.inPurgeable = true;
                     Bitmap b = BitmapFactory.decodeStream(inStream, null, options);

                     mImageCollection[i]=b;


                i++;
            }
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {

            ImageView i = new ImageView(mContext);

            i.setImageBitmap(mImageCollection[position]);
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(136, 88));
            return i;
        }

        private Context mContext;
        private String[] mImageURLs = {};
        private Bitmap[] mImageCollection = {};


    }

スレッドにないため、これはエラーをスローします。URL の読み取りと画像がバックグラウンドで読み込まれるように、このコードを変更するにはどうすればよいですか?

そのため、バックグラウンド スレッドとキャッシュを処理する SmartImageView を使用して ImageAdapter を変更しました。

public class ImageAdapter extends BaseAdapter {
       List<ImageGallery> glist = null;
       private String url;
        public ImageAdapter(Context c,List<ImageGallery> lst) {
            mContext = c;
            glist = lst;
            int i=0;
            al = new ArrayList<String>(); 
            for (ImageGallery id : glist) {

                al.add(id.getImageURL());

            }   
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {
           Log.d("deepak", "getview gallery");
            SmartImageView i = new SmartImageView(mContext);
            i.setImageUrl(al.get(position));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(136, 88));
            return i;
        }

        private Context mContext;
        private String[] mImageURLs = {};
        private ArrayList<String> al; 
        private Bitmap[] mImageCollection = {};
        private Integer[] mImageIds = {};

    }

しかし、私の getView() は今呼び出されていません。

4

2 に答える 2

0

Smart Image ビューを利用できます。SmartImageView は、Android の標準 ImageView のドロップイン代替品であり、さらに URL またはユーザーの連絡先アドレス帳から画像を読み込むことができます。画像はメモリとディスクにキャッシュされ、超高速の読み込みが可能です。詳細については、次のリンクを参照してくださいhttps://github.com/loopj/android-smart-image-view .これがあなたのタスクを達成するのに役立つことを願っています

于 2012-11-30T17:40:17.800 に答える
0

AsyncImageLoader クラスを作成し、http からの画像のダウンロードを処理することをお勧めします。このようにして、すべてを個別のスレッドでキャッシュして管理し、読み込みが完了したら画像をビューに設定できます。また、画像を別の場所にダウンロードする場合は、アプリケーション全体でこのクラスを使用できます。

アダプターのようなものを呼び出すことができmImageLoader.loadImage(myImageView, Url)、ロードが完了するとドロップされます。

詳細が必要な場合はお知らせください:)

于 2012-11-30T18:30:04.163 に答える