Android Imitation micro-letter image Selector _android

Source: Internet
Author: User

Many projects need to use the picture selection control, each time to write a lot of logic. A control Photouploadview is encapsulated based on the picture selection component (Photopicker). Easy and easy, one-click Integration, a few lines of code can be added like a micro-letter image selection control. Here's a look at some of the features of the control and how to use it. First look at the picture:

Effects such as the above, click the Plus popup selection box, currently provides two forms, one as seen in the Popupwindow, the other is Materialdialog, choose to take photos or from the library to obtain, from the image library to enter figure two, After the selection of figure three or figure four here because a lot of items need to be different, so special package: Tu San: Upload success after the progress prompted hidden and failed to repeat the hint hidden, or bulk upload does not need progress prompts and failure to repeat. Figure four: The selection after the traversal upload, there is a possibility of a upload failed. Click on the Reset button in the image to trigger the postback event of course if you feel the deletion and re-hair of the icon ugly, completely can replace it yourself, delete the icon called Icon_photo_view_cancel, the icon_photo_view_resend icon called, Just follow this name and place the Mipmap package underneath your project, and the control will automatically replace it without having to set anything.

First of all, you can add dependencies on your project Build.gradle compile ' com.github.bmobsnail:photouploadview:1.0.0 '
or in my GitHub download library and then import. Then use the change control in your layout, 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"/>

Look at these attributes are not very familiar with, yes! It is the base of the GridView, so it can be used as a gridview,gridview some of it also has, easy to understand it. After adding this step you can see in the layout a control with a plus sign as shown in Figure one. And then there's the rhythm of writing code. Control can choose whether to select the original path or the compressed path, so there is a way to set the cache path the cache path is initialized in your application, and later compressed images are slow to exist under this path.

String cache = Storageutils.getowncachedirectory (this, "Upload/photo"). GetAbsolutePath ();
Picturelibrary.init (this, cache + "/");

followed by the use of the control to perform initialization init, the reason for this initialization step is that there is a project need to edit the sent successful with the picture of the post, those pictures also want to display in the selection of picture control, so there is a fifth parameter, get to the collection of picture data, can display those network pictures, At this point you can remove the add like normal use. Setshowmethod is using that style to show the bomb frame

Mpictureuploadview = (Pictureuploadview) Findviewbyid (R.id.pictureuploadview);
The first parameter context
//second parameter has chat (returns the original image path), UPLOAD (returns the compression path)
///The third parameter select number//fourth parameter default is True (may not fill), false is does not show upload progress
// The fifth parameter defaults to the data collection list (not filled)
mpictureuploadview.init (This, Uploadpichelper.upload, 3, true);
Mpictureuploadview.setshowmethod (Pictureuploadview.popupwindow);//popupwindow, Dialogmpictureuploadview.setuploadcallback (this);

Set the callback method and implement Pictureuploadview.uploadcallback and rewrite two methods, add callback and delete callback, Muploadmap save successful Hashmap,mlocalmap for local records after successful upload HashMap, After the upload success and upload failure according to the map of the tag to determine which picture of the postback button to display or hidden.

Private map<string, string> Muploadmap, Mlocalmap; 

Add picture 
@Override public 
void Onaddcallback (string path, string tag) { 
 mlocalmap.put (tag, path); 
 Upload (path, tag); 

Delete picture 
@Override public 
void onRemoveCallback (String tag) { 
 muploadmap.remove (tag); 
 Mlocalmap.remove (tag);


Finally rewrite this onactivityresult, do not rewrite not unless the above two callback methods, so you can not see the picture added

@Override 
protected void onactivityresult (int requestcode, int resultcode, Intent data) { 
 if (ResultCode = = Resul T_OK) { 
 mpictureuploadview.setresult (Requestcode, ResultCode, data); 
 } 
Super.onactivityresult (Requestcode, ResultCode, data); 
}

Only need the above steps can be implemented like the micro-letter of the picture selection control, the other upload callback processing reference github inside the demo, interested friends can try, welcome to correct.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.