Android类似微信图片选择器
ºÜ¶àÏîÄ¿ÒªÓõ½Í¼Æ¬Ñ¡Ôñ¿Ø¼þ£¬Ã¿´Î¶¼ÒªÐ´Ò»´ó¶ÑÂß¼¡£ÓÚÊÇ»ùÓÚͼƬѡÔñ×é¼þ£¨PhotoPicker£©·â×°ÁËÒ»¸ö¿Ø¼þPhotoUploadView¡£
·½±ã¼òÒ×£¬Ò»¼ü¼¯³É£¬¼¸¾ä´úÂë¾Í¿ÉÒÔÌí¼ÓÀàËÆ΢ÐŵÄͼƬѡÔñ¿Ø¼þÁË¡£
ÏÂÃæ½éÉÜһϸÿؼþÓÐЩʲôÌصãÒÔ¼°ÔõôʹÓá£
ÏÈ¿´Í¼£º
Ч¹ûÈçÉÏͼ£¬µã»÷¼ÓºÅµ¯³öÑ¡Ôñ¿ò£¬Ä¿Ç°ÌṩÁËÁ½ÖÖÐÎʽ£¬Ò»¸öÈçͼËù¼ûµÄPopupWindow£¬ÁíÒ»¸öÊÇMaterialDialog£¬Ñ¡ÔñÅÄÕÕ»òÕß´Óͼ¿â»ñÈ¡£¬´Óͼ¿â»ñÈ¡ºó¾Í½øÈëͼ¶þ£¬Ñ¡ÔñÍêÖ®ºó¾ÍͼÈý»òͼËÄ
ÕâÀïÒòΪºÜ¶àÏîÄ¿ÐèÒª²»Ò»Ñù£¬ËùÒÔÌرð·â×°ÁË£º
ͼÈýµÄÇé¿ö£ºÉÏ´«³É¹¦Ö®ºó½ø¶ÈÌáʾÒþ²ØºÍʧ°ÜÖØ·¢ÌáʾÒþ²Ø£¬»òÕßÊÇÅúÁ¿ÉÏ´«²»ÐèÒª½ø¶ÈÌáʾµÄºÍʧ°ÜÖØ·¢µÄ¡£
ͼËĵÄÇé¿ö£ºÑ¡ÔñÍêºó±éÀúÉÏ´«£¬ÓпÉÄܳöÏÖijһÕÅÉÏ´«Ê§°Ü¡£µã»÷ͼÖеÄÖØ·¢°´Å¥´¥·¢ÖØ·¢µÄʼþ
µ±È»Èç¹ûÄã¾õµÃɾ³ýºÍÖØ·¢µÄiconÄÑ¿´£¬ÍêÈ«¿ÉÒÔ×Ô¼ºÈ¡´úÌæ»»Ëü£¬É¾³ýµÄͼ±ê½Ðicon_photo_view_cancel£¬ÖØ·¢µÄͼ±ê½Ðicon_photo_view_resend£¬Ö»ÐèÒª°´ÕÕÕâ¸öÃüÃû£¬È»ºó·ÅÔÚ×Ô¼ºÏîÄ¿µ×ϵÄmipmap°ü£¬¿Ø¼þ»á×Ô¶¯Ìæ»»£¬²»ÐèҪȥsetʲô¡£
ÏÈ˵ÒÀÀµ£¬¿ÉÒÔÔÚÄãÏîÄ¿µÄbuild.gradleÌí¼ÓÒÀÀµ
compile 'com.github.BmobSnail:PhotoUploadView:1.0.0'11
ÓÖ»òÕßÔÚÎÒµÄgithubÏÂÔØ¿âÏÂÀ´ÔÙµ¼Èë¡£
È»ºóÔÚÄãµÄ²¼¾ÖÓÃʹÓøĿؼþ£¬PictureUploadView
<upload.view.PictureUploadView android:id="@+id/pictureUploadView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/partition_normal"
android:horizontalSpacing="2dp"
android:numColumns="3"
android:scrollbars="none"
android:verticalSpacing="1dp" />123456789123456789
¿´×ÅÕâЩÊôÐÔÊDz»ÊǺÜÊìϤ£¬Ã»´í£¡Ëü¾ÍÊÇ»ù´¡GridViewµÄ£¬ËùÒÔÍêÈ«¿ÉÒÔµ±×÷GridView£¬GridViewÓеÄËüÒ²ÓУ¬¼òµ¥Ò׶®ÁË°É¡£Ìí¼ÓÍêÕâÒ»²½Äã¾Í¿ÉÒÔÔÚ²¼¾Ö¿´µ½Í¼Ò»ÄÇÑùÓиö¼ÓºÅµÄ¿Ø¼þÁË¡£È»ºó¾ÍÊÇд´úÂëµÄ½Ú×àÁË¡£¿Ø¼þ¿ÉÒÔÑ¡ÔñÉèÖÃÑ¡ÔñÔͼ·¾¶»¹ÊÇѹËõºóµÄ·¾¶£¬Òò´Ë¾ÍÓÐÁËÉèÖûº´æ·¾¶ÕâÒ»²½
ÔÚÄãµÄapplicationÖгõʼ»¯Ò»Ï»º´æ·¾¶£¬ÒÔºóµÄѹËõºóµÄͼƬ¶¼»º´æÔÚÕâ·¾¶µ×ÏÂ
String cache = StorageUtils.getOwnCacheDirectory(this, "Upload/photo").getAbsolutePath();
PictureLibrary.init(this, cache + "/");1212
½ô½ÓמÍÊÇʹÓÃÁË£¬¿Ø¼þÖ´Ðгõʼ»¯init£¬Ö®ËùÒÔ»¹ÒªÒ»´ÎÕâÑù³õʼ»¯²½ÖèÊÇÓÐÏîÄ¿ÐèÇóÊÇÒª±à¼ÒÑ·¢Ëͳɹ¦µÄ´øͼƬµÄÌû×Ó£¬ÄÇЩͼҲҪÏÔʾÔÚÑ¡ÔñͼƬ¿Ø¼þÉÏ£¬ËùÒÔÓеÚÎå¸ö²ÎÊý£¬»ñÈ¡µ½Í¼Æ¬Êý¾ÝµÄ¼¯ºÏºó´«½øÈ¥£¬¾ÍÄÜÏÔʾÄÇЩÍøÂçµÄͼƬ£¬Õâʱºò¿ÉÒÔɾ³ýÌí¼ÓÏñÆÕͨÄÇÑùʹÓÃÁË¡£setShowMethodÊÇʹÓÃÄÇÖÖ·ç¸ñÏÔʾµ¯¿ò
mPictureUploadView = (PictureUploadView) findViewById(R.id.pictureUploadView);//µÚÒ»¸ö²ÎÊýÉÏÏÂÎÄ//µÚ¶þ¸ö²ÎÊýÓÐCHAT(·µ»ØÔͼ·¾¶)£¬UPLOAD(·µ»ØѹËõ·¾¶)//µÚÈý¸ö²ÎÊýÑ¡Ôñ¸öÊý//µÚËĸö²ÎÊýĬÈÏΪtrue(¿É²»Ìî)£¬falseΪ²»ÏÔʾÉÏ´«½ø¶È//µÚÎå¸ö²ÎÊýĬÈÏΪÊý¾Ý¼¯ºÏList(¿É²»Ìî)mPictureUploadView.init(this, UploadPicHelper.UPLOAD, 3, true);
mPictureUploadView.setShowMethod(PictureUploadView.POPUPWINDOW);//POPUPWINDOW£¬DIALOGmPictureUploadView.setUploadCallBack(this);123456789123456789
ÉèÖÃһϻص÷·½·¨²¢ÊµÏÖPictureUploadView.UploadCallBack²¢ÖØдÁ½¸ö·½·¨£¬Ìí¼ÓµÄ»Øµ÷ºÍɾ³ýµÄ»Øµ÷£¬mUploadMapΪÉÏ´«³É¹¦Ö®ºó±£´æ³É¹¦µÄhashMap£¬mLocalMapΪ±¾µØ¼Ç¼Ìí¼ÓºÍɾ³ýµÄhashMap£¬Ö®ºóµÄÉÏ´«³É¹¦ºÍÉÏ´«Ê§°ÜÒª¸ù¾ÝÕâ¸ömapµÄtagÀ´¾ö¶¨ÄĸöͼƬµÄÖØ·¢°´Å¥¸ÃÏÔʾ»¹ÊÇÒþ²ØµÄ¡£
private Map<String, String> mUploadMap, mLocalMap; //Ìí¼ÓͼƬ
@Override public void onAddCallback(String path, String tag) {
mLocalMap.put(tag, path); //upload(path, tag);
} //ɾ³ýͼƬ
@Override public void onRemoveCallback(String tag) {
mUploadMap.remove(tag);
mLocalMap.remove(tag);
}1234567891011121314151612345678910111213141516
×îºóÔÙÖØдһÏÂÕâ¸öonActivityResult£¬²»ÖØд²»»á³ý·ÇÉÏÃæÁ½¸ö»Øµ÷·½·¨£¬ËùÒԾͿ´²»µ½ÓÐͼƬÌí¼Ó
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) {
mPictureUploadView.setResult(requestCode, resultCode, data);
} super.onActivityResult(requestCode, resultCode, data);
}12345671234567
Ö»ÐèÒªÉÏÃæÕ⼸²½¾Í¿ÉÒÔʵÏÖÏñ΢ÐŵÄÄǸöͼƬѡÔñ¿Ø¼þÒ»ÑùÁË£¬ÆäËüÉÏ´«»Øµ÷µÄ´¦Àí²Î¿¼githubÀïÃæµÄdemo£¬ÓÐÐËȤµÄÅóÓÑ¿ÉÒÔÊÔÊÔ£¬»¶Ó´ó¼ÒÖ¸Õý