1

DB、さまざまな文字列、および外部ディレクトリの画像への画像パスを表す文字列からデータを抽出しています。

私の問題は、リストビューがカーソルオブジェクトからすべての画像をロードするときに、時間がかかる最初のリストビューにロードされ、その後、各画像が後続のビューに正しくロードされることです。

私の質問は、データベースからのすべての画像が最初のリスト ビューに読み込まれ、必要に応じてリスト ビューごとに 1 つの画像が読み込まれない理由です。

これは私の Cursor アダプター コードです。非同期タスクを使用して DB から画像を読み込みます (コードの割り当てを事前にお詫びします!)。

任意の入力をいただければ幸いです。

 @Override
        public void bindView(View v, Context context, final Cursor c) {


            /*
             * Binds the data from each row (stored in cursor object) to the ListView.xml
             * first free up List object if not in view of the screen by holding a ref to it, therefore dont
            * waste memeory and tiome recreating each view when out of viewable list to user
            */

            ViewHolder holder = (ViewHolder) v.getTag();

            String diveSite = c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY_DIVESITE));
            holder.title_text.setText(diveSite);

            String date = c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY__DIVEDATE));
            holder.date_text.setText(date);

            String rating = c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY_DIVERATING));
            holder.bar.setNumStars(5);
            holder.bar.setRating( Float.parseFloat(rating));




            String diveNumber= c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY__DIVENUMBER));


            String diveImagePath = c.getString(c.getColumnIndex(diveDataBase.KEY_DIVEPICTURE));


            c.moveToLast();
            noOfRows = Integer.parseInt(c.getString(c.getColumnIndex(diveDataBase.KEY__DIVENUMBER)));
            holder.dive_no.setText(diveNumber+"/"+noOfRows);



            //set image here once taken form external string path, and resized bitmap conversion

            getImageAsynch = (getBitmapImage) new getBitmapImage(v).execute(diveImagePath);

これは、パスからextディレクトリへの画像が取得され、サイズが変更され、ビューホルダーに入力される非同期内部クラスです...

class getBitmapImage extends AsyncTask{

            private View view;
            private ViewHolder holderB;
            public getBitmapImage(View v) {
            // TODO Auto-generated constructor stub, takes ViewGroup as arg
                //ViewGroup parent;
                view=v;
                holderB=new ViewHolder();

        }





            @Override
            protected Bitmap doInBackground(String... imagePath) {

                /* 
                 * get image path and decode to bitmap
                *   First must make sure image loaded from DB base 64 is not loaed into memory 
                *   at full size ie 1028 * 800 pixels
                *   Instead we use BitMapOptions object methods inJustDecodeBounds 
                *   to stop autoloading of image, 
                *   then we scale down the image for loading into memory using 
                *   BitMapOPtiontions.inSampleSize method
                *   This will significantly reduce memory usage and time req to load images into list view
                *
                *    first check if user wants to preview images (boolean displayImagesUserChoice), if not return null, 
                *   this value is passed from dialog propmt in ViewListOfDives
                *   and passed to ItemAdpter constructor
                */


                if(displayImagesUserChoice){

                if(!isCancelled()){
                String diveImagePath = imagePath[0];

                 File imagePathFile = new File(diveImagePath); 
                 try {
                    final int IMAGE_MAX_SIZE = 3000;
                        FileInputStream streamIn = new FileInputStream(imagePathFile);

                    // Decode image size and setInJBounds = true to avoid auto memory allocation for large image
                        BitmapFactory.Options o = new BitmapFactory.Options();
                        o.inJustDecodeBounds = true;
                       BitmapFactory.decodeStream(streamIn, null, o);
                         streamIn.close();

                        -----allot of resizing image code here have removed for an easier read---

                         streamIn.close();
                         b.recycle();
                         System.gc();

                } else {
                    bitmap = BitmapFactory.decodeStream(streamIn);
                   resizedImage = reSizeImage(bitmap);
                  streamIn.close();
                 noOfImagesloaded++;
                   System.gc();
                }

                        //resizedImage = reSizeImage(bitmap);


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



            }catch(OutOfMemoryError exc){
                exc.printStackTrace();
                //Toast.makeText(this, "Something went wrong! Try again...", Toast.LENGTH_SHORT).show();
            }catch(NullPointerException nullpoint){
                nullpoint.printStackTrace();
                //end try catch
            }


                }//end if anstch class notCancelled from the onCakPressed method of viewListOfDives
                else if(isCancelled()){
                    Log.d("ItemAdpter Aycnh", "Do in background cancelled");
                }

                Log.d("ItemApdter Aycnh", "No of Images loaded = "+  noOfImagesloaded);
                //return bitmap;

                }//end if displayImagesUserChoice=true, if not true resizedIMage is returned as null to onPostExecute

                return resizedImage;
            }//end do in background


            @Override
            protected void onPostExecute(Bitmap bitmap) {

                //intilise holder to listview.xml ImageView
                holderB.displayImage = (ImageView) view.findViewById(R.id.iv_list_image); //image view
                //ImageView displayImage = (ImageView) view.findViewById(R.id.iv_list_image);
                if(bitmap!=null ){

                    //set image using holder static object

                    holderB.displayImage.setBackground(null);

                    holderB.displayImage.setImageBitmap(resizedImage);

                }else{


                    //set default image using static holder object

                    holderB.displayImage.setBackgroundResource(R.drawable.camera4);
                }

            }//end onPOstExecute
        }//end getBitmap asynch
4

1 に答える 1

0

テキスト ビューと評価バーを含むビュー ホルダーは、SD カードから非同期に取得された画像よりも速く返されるため、ビューに画像を取り込むのに遅延が生じます。イメージの非同期ロードを削除すると問題は解決しますが、GUI はすべてのイメージがロードされるまで応答しないため、非同期タスクを使用する以外に選択肢はありません。

于 2014-10-11T22:07:11.097 に答える