如何从arraylist android加载图库图像

时间:2012-02-07 03:10:54

标签: android android-layout android-gallery

我有一个动态值的图像存储在我想要在图库中显示的arraylist缩略图中。 arraylist中的字符串值是缩略图的图像路径(例如/mnt/sdcard/MyApp/Thumbnails/thumb1.png)....

我有这段代码:

class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private ArrayList<String> thumbnails = new ArrayList<String>();


    public ImageAdapter(Context c, ArrayList<String> thumb) {
        mContext = c;
        System.out.print("thumbs: " + thumb);
        thumbnails = thumb;
    }

    public int getCount() {
        return thumbnails.size();
    }

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

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



   public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView = new ImageView(mContext);
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inSampleSize = 0;
      Bitmap bm = BitmapFactory.decodeFile(thumbnails.get(i), options);
      imageView.setImageBitmap(bm); 
      imageView.setLayoutParams(new LayoutParams(150, 150));
      imageView.setScaleType(ImageView.ScaleType.FIT_XY);
      return imageView;
    }
}

以及从主类显示图库视图的代码/方法:

Gallery gal1;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    gal1 = (Gallery) this.findViewById(R.id.gallery1);
}

private void setDisplay(){

    OnItemClickListener myItemClickListener = new OnItemClickListener() {
      @Override
      public void onItemClick (AdapterView<?> parent, View v, int position, long id) {
        Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
      };
    };

  gal1.setAdapter(new ImageAdapter(this, _paththumb));
}

如何从arraylist加载图片?

更新: 我用你的代码zrgui和logcat说:

02-07 12:31:56.046: E/AndroidRuntime(3572): FATAL EXCEPTION: main
02-07 12:31:56.046: E/AndroidRuntime(3572): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.Gallery.setUpChild(Gallery.java:769)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.Gallery.makeAndAddView(Gallery.java:738)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.Gallery.layout(Gallery.java:622)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.Gallery.onLayout(Gallery.java:336)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.view.View.layout(View.java:7035)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1238)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1044)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.view.View.layout(View.java:7035)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.view.View.layout(View.java:7035)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.ScrollView.onLayout(ScrollView.java:1205)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.view.View.layout(View.java:7035)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.view.View.layout(View.java:7035)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.view.View.layout(View.java:7035)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.view.View.layout(View.java:7035)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.os.Looper.loop(Looper.java:123)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at java.lang.reflect.Method.invoke(Method.java:521)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-07 12:31:56.046: E/AndroidRuntime(3572):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

如果初始化ImageAdapter时在_paththumb中有图像路径,那么它应该都可以。另外不要忘记调用setDisplay()。

您还应该优化getView方法,因为您正在创建新的ImageView而不是重用它们(从而使适配器的整个目的无效)。

编辑:getView优化:

  public View getView(int position, View convertView, ViewGroup parent) {
          ImageView imageView = (ImageView)convertView;
          if (imageView == null) {
             imageView = new ImageView(mContext);
          }
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inSampleSize = 0;
          Bitmap bm = BitmapFactory.decodeFile(thumbnails.get(i), options);
          imageView.setImageBitmap(bm); 
          imageView.setLayoutParams(new LayoutParams(150, 150));
          imageView.setScaleType(ImageView.ScaleType.FIT_XY);
          return imageView;
    }