Open fred-ye opened 9 years ago
分享一个之前写的一个选择图片的Feature.
public class PickPictureActivity extends Activity { private static final int CODE_OPEN_GALLERY = 1; final static int KITCAT_REQUEST_PICK = 1001; private static final String TAG = "PickPictureActivity"; private ArrayList<String> imageUrls = new ArrayList<String>(); private GridView gvImages; public ImageLoader imageLoader = ImageLoader.getInstance(); private PickPictureAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pick_picture); gvImages = (GridView) findViewById(R.id.gv_add_pic); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) .memoryCache(new WeakMemoryCache()).build(); imageLoader.init(config); adapter = new PickPictureAdapter(PickPictureActivity.this); gvImages.setAdapter(adapter); Log.i("Activity", "onCreate execute"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); imageUrls = savedInstanceState.getStringArrayList("imageUrls"); } /** * Pick a existing picture from gallery. */ public void pickPhotoFromGalley() { Log.i("PicPicture", "pic size" + imageUrls.size()); if (android.os.Build.VERSION.SDK_INT < 19) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("return-data", true); startActivityForResult(intent, CODE_OPEN_GALLERY); } else { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult(intent, KITCAT_REQUEST_PICK); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.i("Activity", "onActivityResult size: " + imageUrls.size() ); super.onActivityResult(requestCode, resultCode, data); if (requestCode != RESULT_CANCELED && null != data) { Uri uri = data.getData(); Log.i(TAG, "uri:" + uri); if (null != getRealPathFromURI(uri)) { Log.i(TAG, getRealPathFromURI(uri)); imageUrls.add("file://" + getRealPathFromURI(uri)); adapter.clearItems(); adapter.addAllItems(imageUrls); gvImages.setAdapter(adapter); } } } public String getRealPathFromURI(Uri contentUri) { Cursor cursor = null; try { String[] proj = { MediaStore.Images.Media.DATA }; cursor = getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } finally { if (cursor != null) { cursor.close(); } } } @Override protected void onSaveInstanceState(Bundle outState) { outState.putStringArrayList("imageUrls", imageUrls); super.onSaveInstanceState(outState); Log.i("Activity", "onSaveInstanceState execute"); } }
值得说明的几点
Activity
onSaveInstanceState(Bundle outState)
public class PickPictureAdapter extends BaseAdapter { private Context context; private List<String> imageList = new ArrayList<String>(); private ViewHolder holder; public PickPictureAdapter(Context context) { this.context = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (null == convertView) { holder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.pick_picture_item, null, false); holder.ivAddPic = (ImageView)convertView.findViewById(R.id.iv_add_pic); holder.ivPic = (ImageView)convertView.findViewById(R.id.iv_my_image); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (position == imageList.size()) { holder.ivAddPic.setVisibility(View.VISIBLE); holder.ivPic.setVisibility(View.GONE); holder.ivAddPic.setOnClickListener(listener); } else { holder.ivPic.setVisibility(View.VISIBLE); holder.ivAddPic.setVisibility(View.GONE); if (null != getItem(position)) { ((PickPictureActivity) context).imageLoader.displayImage(imageList.get(position), holder.ivPic); } } return convertView; } private View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View arg0) { PickPictureActivity activity = (PickPictureActivity) context; activity.pickPhotoFromGalley(); } }; class ViewHolder { public ImageView ivPic; public ImageView ivAddPic; } @Override public int getCount() { Log.i("Adapter", "image count:" + imageList.size()); return imageList.size() + 1; } @Override public Object getItem(int position) { if (getCount() > 0) { return imageList.get(position); } return null; } @Override public long getItemId(int arg0) { return 0; } public void clearItems() { imageList.clear(); notifyDataSetChanged(); } public void addAllItems(List<String> refreshList) { imageList.addAll(refreshList); notifyDataSetChanged(); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/iv_my_image" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/icon_default" android:scaleType="fitXY" android:visibility="gone" /> <ImageView android:id="@+id/iv_add_pic" android:layout_width="80dp" android:layout_height="80dp" android:padding="20dp" android:background="@drawable/pick_picture_normal" android:src="@drawable/icon_add" android:visibility="gone" /> </RelativeLayout>
针对添加按钮周围的边框
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="@null" /> <stroke android:dashGap="4dp" android:dashWidth="8dp" android:width="2dp" android:color="#c2c2c2" /> </shape>
分享一个之前写的一个选择图片的Feature.
Activity 类
值得说明的几点
Activity
销毁,为此需要覆盖其onSaveInstanceState(Bundle outState)
方法,否则有可能在选好图片后,返回到当前Activity时发现之前选的图片丢失了。Adapter
看一下我们的 pic_picture_item.xml
样式定义pick_picture_normal.xm
针对添加按钮周围的边框
上图