前2篇都是从资源文件里面的直接读取出来的,现在实现读取本地SDcard里面的图片:
源码修改
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
findData();
}
布局文件为简单的ListView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </RelativeLayout>重点是findData()这个函数,不废话直接上代码:
/** * 找到本地的所有的图片 */ @SuppressLint("InlinedApi") private void findData() { mImageLoader = new ImageLoader(); Cursor c = getContentResolver().query(DataConfig.PHOTO_URI, null, null, null, null); if (null != c && c.moveToFirst()){ do{ MyPhoto myPhoto = new MyPhoto(); String path = c.getString(c.getColumnIndex(MediaStore.Images.Media.DATA)); if (!TextUtils.isEmpty(path)){ myPhoto.setPath(path); } String displayName = c.getString(c.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)); if (!TextUtils.isEmpty(displayName)){ myPhoto.setDisplayName(displayName); } String type = c.getString(c.getColumnIndex(MediaStore.Images.Media.MIME_TYPE)); if (!TextUtils.isEmpty(type)){ myPhoto.setType(type); } String title = c.getString(c.getColumnIndex(MediaStore.Images.Media.TITLE)); if (!TextUtils.isEmpty(title)){ myPhoto.setTitle(title); } int size = c.getInt(c.getColumnIndex(MediaStore.Images.Media.SIZE)); if (size > 0){ myPhoto.setSize(size); } int width = c.getInt(c.getColumnIndex(MediaStore.Images.Media.WIDTH)); if (width > 0){ myPhoto.setWidth(width); } int height = c.getInt(c.getColumnIndex(MediaStore.Images.Media.HEIGHT)); if (height > 0){ myPhoto.setHeight(height); } DataConfig.myArrayList.add(myPhoto); }while(c.moveToNext()); } }
public class MyPhoto { private String path; private String displayName; private String type; private String title; private int size; private int width; private int height; public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getDisplayName() { return displayName; } public void setDisplayName(String displayName) { this.displayName = displayName; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } @Override public String toString() { return "MyPhoto [path=" + path + ", displayName=" + displayName + ", type=" + type + ", title=" + title + ", size=" + size + ", width=" + width + ", height=" + height + "]"; } }
DataConfig.java 记录一下程序要使用的全局变量
public class DataConfig { public static Uri PHOTO_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; public static ArrayList<MyPhoto> myArrayList = new ArrayList<MyPhoto>(); }
public class MyAllPhotoAdapter extends BaseAdapter{ protected static final String TAG = "MyAllPhoteAdapter"; Context mContext = null; ArrayList<MyPhoto> mList = null; public MyAllPhotoAdapter(ArrayList<MyPhoto> list , Context context){ mList = list; mContext = context; } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int arg0) { return mList.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } ViewHolder mHolder = null; @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder mHolder = null; if (null == view){ view = LayoutInflater.from(mContext).inflate(R.layout.photo_list, null); mHolder = new ViewHolder(); mHolder.mImage = (ImageView)view.findViewById(R.id.mImage); mHolder.mCheckBox = (CheckBox)view.findViewById(R.id.mCheckBox); mHolder.mText = (TextView)view.findViewById(R.id.mText); mHolder.mImage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LogUtils.i(TAG , "mHolder.mImage is clicked"); } }); mHolder.mCheckBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LogUtils.i(TAG , "mHolder.mCheckBox is clicked"); } }); mHolder.mText.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LogUtils.i(TAG , "mHolder.mText is clicked"); } }); view.setTag(mHolder); }else{ mHolder = (ViewHolder)view.getTag(); } mHolder.mText.setText(mList.get(position).getDisplayName()); // mHolder.mCheckBox.setVisibility(View.GONE); MyPhotoAsyncTask myPhotoAsyncTask = new MyPhotoAsyncTask(mHolder.mImage); myPhotoAsyncTask.execute(mList.get(position).getPath()); return view; } public class ViewHolder{ public ImageView mImage = null; public CheckBox mCheckBox = null; public TextView mText = null; } public class MyPhotoAsyncTask extends AsyncTask<String, Void, Bitmap>{ ImageView mImageView = null; public MyPhotoAsyncTask(ImageView imageView){ mImageView = imageView; } @Override protected Bitmap doInBackground(String... path) { Bitmap bitmap = ImageLoader.decodeSampledBitmapFromResource(path[0], 90, 90); return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { if(null != mImageView && null != bitmap){ mImageView.setImageBitmap(bitmap); } } } }布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" > <!-- android:descendantFocusability="blocksDescendants" --> <ImageView android:id="@+id/mImage" android:layout_width="90dp" android:layout_height="90dp" android:scaleType="fitXY" android:src="@drawable/empty_photo"/> <TextView android:layout_marginLeft="110dp" android:id="@+id/mText" android:layout_width="wrap_content" android:layout_height="fill_parent" android:singleLine="true" android:layout_centerVertical="true" android:background="#ff00ff55" /> <CheckBox android:id="@+id/mCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:layout_centerInParent="true" /> </RelativeLayout>
android:focusable="false"
想ImageView textVIew 本身就默认是false 就可以不设置了
方法2:
Item的布局文件的最外层layout设置
android:descendantFocusability="blocksDescendants" 让子控件得不到焦点的意思
<p><span style="font-family:SimSun;font-size:18px;">属性的值有三种:</span></p><p><span style="font-family:SimSun;font-size:18px;"> beforeDescendants:viewgroup会优先其子类控件而获取到焦点</span></p><p><span style="font-family:SimSun;font-size:18px;"> afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点</span></p><p><span style="font-family:SimSun;font-size:18px;"> blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点</span> </p>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:descendantFocusability="blocksDescendants" > <!-- android:descendantFocusability="blocksDescendants" --> <ImageView android:id="@+id/mImage" android:layout_width="90dp" android:layout_height="90dp" android:scaleType="fitXY" android:src="@drawable/empty_photo"/> <TextView android:layout_marginLeft="110dp" android:id="@+id/mText" android:layout_width="wrap_content" android:layout_height="fill_parent" android:singleLine="true" android:layout_centerVertical="true" android:background="#ff00ff55" /> <CheckBox android:id="@+id/mCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:focusable="false" android:layout_centerInParent="true" /> <Button android:id="@+id/mButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="100dp" android:layout_centerVertical="true" android:text="详细信息"/> </RelativeLayout>在MyAllPhotoAdapter中增加点击处理事件:
mHolder.mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDetailInfo(position); } });显示一个对话框:
// 显示图片的一些相关信息 private void showDetailInfo(int position) { MyPhoto photo = mList.get(position); PhotoDailog mDailog = new PhotoDailog(mContext, R.style.dialog , photo); mDailog.show(); }关键是 PhotoDialog,源码在下
public class PhotoDailog extends Dialog{ private String path; private String displayName; private String type; private String title; private int size; private int width; private int height; private Context mContext = null; private MyPhoto mPhoto; private ImageView logo = null; private TextView mTitle = null; private TextView mPath = null; private TextView mName = null; private TextView mSize = null; private TextView mType = null; private TextView mWidth = null; private TextView mHeight = null; public String getPath() { return path; } public void setPath(String path) { this.path = path; LogUtils.i("PhotoDailog setPath " + " mContext = " + mContext + "path = " + path); if (null != mPath){ LogUtils.i("PhotoDailog setPath " + " mContext = " + mContext + "path = " + path); mPath.setText(mContext.getString(R.string.path) + path); } } public String getDisplayName() { return displayName; } public void setDisplayName(String displayName) { LogUtils.i("PhotoDailog setDisplayName " + " mContext = " + mContext + "displayName = " + displayName); this.displayName = displayName; if (null != mName){ LogUtils.i("PhotoDailog setDisplayName " + " mContext = " + mContext + "displayName = " + displayName); mName.setText(mContext.getString(R.string.displayname) + displayName); } } public String getType() { return type; } public void setType(String type) { LogUtils.i("PhotoDailog setType " + " mContext = " + mContext + "type = " + type); this.type = type; if (null != mType){ mType.setText(mContext.getString(R.string.type) + type); } } public String getTitle() { return title; } public void setTitle(String title) { LogUtils.i("PhotoDailog setTitle " + " mContext = " + mContext + "title = " + title); this.title = title; if (null != mTitle){ mTitle.setText(title); mTitle.setTextColor(Color.RED); } } public int getSize() { return size; } public void setSize(int size) { this.size = size; if (null != mSize){ mSize.setText(mContext.getString(R.string.size) + size); } } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; if (null != mWidth){ mWidth.setText(mContext.getString(R.string.width) + width); } } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; if (null != mHeight){ mHeight.setText(mContext.getString(R.string.height) + height); } } public void setLog(Bitmap log){ if (null != logo){ logo.setImageBitmap(log); } } public PhotoDailog(Context context, int theme, MyPhoto photo) { super(context, theme); LogUtils.i("PhotoDailog " + " mContext = " + mContext); mContext = context; mPhoto = photo; } @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.dialogview1); LogUtils.i("PhotoDailog " + " onCreate "); logo = (ImageView)findViewById(R.id.logo); mTitle = (TextView)findViewById(R.id.dialog_title); mPath = (TextView)findViewById(R.id.path); mName = (TextView)findViewById(R.id.dispalyname); mType = (TextView)findViewById(R.id.type); mWidth = (TextView)findViewById(R.id.width); mHeight = (TextView)findViewById(R.id.height); mSize = (TextView)findViewById(R.id.size); setLog(ImageLoader.decodeSampledBitmapFromResource(mPhoto.getPath(), 90, 90)); setTitle(mPhoto.getTitle()); setPath(mPhoto.getPath()); setDisplayName(mPhoto.getDisplayName()); setType(mPhoto.getType()); setSize(mPhoto.getSize()); setWidth(mPhoto.getWidth()); setHeight(mPhoto.getHeight()); } }持续更新中...