Android uses the GridView to arrange albums and select images on WeChat

Source: Internet
Author: User

Android uses the GridView to arrange albums and select images

Many projects must use the image upload function, which is even more exciting. Recently, my project also needs to select the image function, so I shared the code I wrote, take notes! Let's take a look:


The effect is good. The Code mainly refers to the remaining helper classes and adapters for the two activities. First, let's take a look at the code on this interface.

Package com. example. choosephotos; import java. io. serializable; import java. util. list; import android. app. activity; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. gridView; public class ChoosePhotosActivity extends Activity {List
 
  
DataList; GridView gridView; ImageBucketAdapter adapter; // custom adapter AlbumHelper helper; public static final String Signature = "imagelist"; public static Bitmap bimap; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. act_image_bucket); helper = AlbumHelper. getHelper (); helper. init (getApplicationContext (); initData (); initView ();}/*** initialization data */private void initData () {dataList = helper. getImagesBucketList (false); bimap = BitmapFactory. decodeResource (getResources (), R. drawable. icon_addpic_unfocused);}/*** initialize view */private void initView () {gridView = (GridView) findViewById (R. id. gridview); adapter = new ImageBucketAdapter (ChoosePhotosActivity. this, dataList); gridView. setAdapter (adapter); gridView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
  Parent, View view, int position, long id) {Intent intent = new Intent (ChoosePhotosActivity. this, ImageGridActivity. class); intent. putExtra (ChoosePhotosActivity. EXTRA_IMAGE_LIST, (Serializable) dataList. get (position ). imageList); startActivityForResult (intent, 100); finish () ;}}) ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resu LtCode, data); if (resultCode! = Activity. RESULT_ OK) {return;} switch (requestCode) {case 100: setResult (Activity. RESULT_ OK); finish (); break; default: break ;}}}
 
Next we will look at the effects of another interface:


Let's take a look at the code on this page:

Package com. example. choosephotos; import java. io. IOException; import java. util. arrayList; import java. util. collection; import java. util. iterator; import java. util. list; import com. example. choosephotos. imageGridAdapter. textCallback; import android. app. activity; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. color; import android. graphics. drawable. colorDrawable; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. adapterView; import android. widget. button; import android. widget. imageView; import android. widget. textView; import android. widget. toast; import android. widget. adapterView. onItemClickListener; import android. widget. gridView; public class ImageGridActivity extends Activity {public static final String EXTRA_IMAGE_LIST = "imagelist"; List
 
  
DataList; GridView gridView; ImageGridAdapter adapter; AlbumHelper helper; Button bt; Handler mHandler = new Handler () {@ Overridepublic void handleMessage (Message msg) {switch (msg. what) {case 0: Toast. makeText (ImageGridActivity. this, "select a maximum of 9 images", 400 ). show (); break; default: break ;}};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. act_image_grid); helper = AlbumHelper. getHelper (); helper. init (getApplicationContext (); dataList = (List
  
   
) GetIntent (). getSerializableExtra (EXTRA_IMAGE_LIST); initView (); bt = (Button) findViewById (R. id. bt); bt. setOnClickListener (new OnClickListener () {public void onClick (View v) {ArrayList
   
    
List = new ArrayList
    
     
(); Collection
     
      
C = adapter. map. values (); Iterator
      
        It = c. iterator (); for (; it. hasNext ();) {list. add (it. next ();} if (Bimp. act_bool) {setResult (Activity. RESULT_ OK); Bimp. act_bool = false;} for (int I = 0; I <list. size (); I ++) {if (Bimp.bmp. size () <9) {try {Bitmap bm1_bimp.revitionimagesize(list.get(i1_1_bimp.bmp. add (bm);} catch (IOException e) {e. printStackTrace () ;}}} finish () ;}});} private void initView () {gridView = (GridView) findViewById (R. id. gridview); gridView. setSelector (new ColorDrawable (Color. TRANSPARENT); adapter = new ImageGridAdapter (ImageGridActivity. this, dataList, mHandler); gridView. setAdapter (adapter); adapter. setTextCallback (new TextCallback () {public void onListen (int count) {bt. setText ("completed" + "(" + count + ")") ;}}); gridView. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
       Parent, View view, int position, long id) {adapter. yydatasetchanged ();}});}}
      
     
    
   
  
 

Now we can determine the size of bimp.bmp. We can only select nine

Well, the effect is complete. The following is the source code:

By the way, do not forget to add permissions:

 
     
     
     
 


Source code download



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.