Pictures of Android Picture Browser Delete

Source: Internet
Author: User

  

the previous article describes how to read the image file from the phone, put in the component GridView implementation Grid Effect of the thumbnail display.

Today, we studied the deletion of children in the GridView (each small picture), refer to the existing software, long press the picture to jump out of the delete confirmation box.

The GridView Long press event is Onitemlongclicklistener (), by the way click for Onitemclicklistener ().

Directly on the code, long press one of the thumbnails in the thumbnail after the delete operation (must have a confirmation box):

1Gridviewimg.setonitemlongclicklistener (NewGridview.onitemlongclicklistener () {//Monitoring Events2              Public BooleanOnitemlongclick (adapterview<?> Parent, view view,intPositionLongID) {3                 FinalView v =view;4                 Final intp =position;5                 FinalBundle savedInstanceState1 =savedinstancestate;6                 NewAlertdialog.builder (mainactivity. This)7 . Settitle (R.string.long_click)8 . Setmessage (r.string.delete_firm)9 . Setpositivebutton (R.string.ok,Ten                         NewDialoginterface.onclicklistener () { One @Override A                              Public voidOnClick (Dialoginterface Dialog,intwhich) { -File RemoveFile =NewFile (Fullpathimg.get (P)); -                                 if(Removefile.exists ()) { the Removefile.delete (); -                                     //Refresh the GridView -                                     //v.setvisibility (v.gone); - adapterimg.notifydatasetchanged (); + finish (); -Intent Intent =NewIntent (mainactivity. This, Mainactivity.class); + startactivity (intent); A  atIntent Media =NewIntent (intent.action_media_scanner_scan_file); -Uri Contenturi =Uri.fromfile (removefile); - Media.setdata (Contenturi); -Mainactivity. This. Sendbroadcast (media); -                                     //gridviewimg.postinvalidate (); -                                     //gridviewimg.invalidate (); in                                     //onCreate (savedInstanceState1); //failed -                                 } to                             } +                         }) - . Setnegativebutton (R.string.cancel, the                         NewDialoginterface.onclicklistener () { * @Override $                              Public voidOnClick (Dialoginterface Dialog,intwhich) {Panax Notoginseng                                 ; -                             } the                         } + ). Show (); A                 return true; the             } +});

While this code is simple to do, there are a lot of details to note:

1, delete the picture in the GridView, the essence is the SD card in the real picture to delete, so involved in the operation of the file, positioning is based on the incoming position parameters;

2, confirm that the dialog box uses Dialoginterface, in its click Response event in the specific implementation can not directly use the GridView long press the parameters passed in, such as the above position,

A system hint is a variable that requires a final decoration. Therefore, the corresponding final variable needs to be redefined at the beginning to satisfy the requirement;

1 Final View v = view; 2 Final int p = position; 3  Final Bundle savedInstanceState1 = savedinstancestate;

3, the beginning of the implementation of the file deletion function, the information is the following lines of code:

1 New File (Fullpathimg.get (P)); 2  if (Removefile.exists ()) {3    removefile.delete (); 4 }

Where Fullpathimg explains that it is the arraylist<string> variable that holds the full path to the picture, and calls its get () method (indexed as an argument) to get the picture path.

It is also important to determine the existence of a file, after which the delete () method is called, but a problem is found:

  The file Manager does not already have the deleted picture name, but the phone comes with the picture browser still can search, the computer opens the phone corresponding folder also exists this file, but is corrupted file?

  This is incredible, and then a check, ask questions, I have to say that most of the online search articles are not mentioned, only to delete ().

Later, in the Android Open source community, the question was answered accurately, after the deletion or preservation of the file, the system needs to be updated, by the form of broadcasting to complete.

The code is very concise:

1 New Intent (intent.action_media_scanner_scan_file); 2 Uri Contenturi = uri.fromfile (removefile); 3 Media.setdata (Contenturi); 4 mainactivity. this. sendbroadcast (media);

This is the four lines of code, the completion of the phone file information synchronization. Of course, it can also be two lines:

  

1 New Intent (Intent.action_media_scanner_scan_file, Uri.fromfile (RemoveFile)); 2 mainactivity. this. sendbroadcast (media);

The knowledge and use of intent and URIs is too wide to be learned slowly.

4, played album Friends should be clear, and then delete a picture, the software will automatically fill that vacancy (update GridView, of course, the phone comes with the software will not, need to reopen).

In fact, there are two things done, first, the elements in the GridView update; second, the main interface update for the GridView is displayed.

The code is as follows:

1 adapterimg.notifydatasetchanged (); 2 finish (); 3 New Intent (mainactivity.  this, mainactivity. class ); 4 startactivity (intent);

The effect of the finish () method is to end the element in the current Activity,gridview is determined by the Imageadapter class, so you only need to send a notification of the data change to the latter (Notifydatasetchanged ()).

The comment section in the code is the method that attempts to update the GridView at the beginning, and does not achieve the desired effect.

Finally, put a picture.

Picture removal for Android Image browser

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.