Android Mediaprovider--file management: must be present, File Manager in a few new folders, after bulk deletion, connected to the computer to see still some folders are not deleted

Source: Internet
Author: User
<span id="Label3"></p><p><p>Problem Description:</p></p><p><p>"test steps": 1, go to the file manager, create a few new folders, and then select the whole batch of these folders-delete;<br>2, the mobile phone connected to the computer, on the computer side view the file display.<br>"test results": the computer side view still has some folders not Deleted. Plug and unplug the USB cable several times, Re-view on the computer side, still Displayed.<br>Expected result: deleted folders should not be displayed on the computer Side.<br>"recurrence probability": 100% (if The first is not reproduced, step 1, 2 re-operation can be reproduced)<br>"notes": Click "search" in the file management, also can search out these Folders.<br>Follow these steps to make it easier to reproduce<br>1. Add a local folder, delete the newly added folder<br>2. Create another Folder. (the folder in the file system has been created and this record has been added to the media library, but the folder that was deleted in action 1 appears again in the media library)</p></p><p><p>This problem is caused by Mediaprovider added thread pool scan, Android native does not have this problem,</p></p><p><p><br></p></p><p><p>Workaround: Modify the internal class of the Packages/providers/mediaprovider/src/com/android/providers/media/mediascannerthreadpool.java file:</p></p><p><p></p></p><pre code_snippet_id="660966" snippet_file_name="blog_20150507_1_9581857" name="code" class="java">Private class Insertfoldertask extends Task {private contentresolver mcontentresolver;//<2015.05.06> Modify For Startprivate boolean isexistinfilesystem (String folderpath) {return new File (folderpath). exists ();}//<2015.0 5.06> Modify for end public Insertfoldertask () {super (priority_max + less_prior, 0); Mcontentresolver = Mcontext.getcontentresolver (); } @Override public void run () {long start = System.currenttimemillis (); int size = Mfoldermap.size (); list<contentvalues> folderlist = new Arraylist<contentvalues> (size); iterator<entry<string, folderstructure>> Iterator = mfoldermap.entryset (). Iterator (); While (iterator.hasnext ()) {entry<string, folderstructure> Entry = Iterator.next (); String FolderPath = Entry.getkey (); Folderstructure folderstructure = Entry.getvalue ();//<2015.05.06> Modify for Start///If folder has been deleted right now, remove from map and do next Check.if (!isexistin FileSystem (folderpath)) { iterator.remove ();  continue; }//<2015.05.06> Modify for End//Root folder path (store in each Directory) ha ve been insert while begin scanning if (!isexistindatabase (folderpath)) {contentvalues values = new Contentvalues (); Values.put (MediaStore.Files.FileColumns.TITLE, Mediafile.getfiletitle (folderpath)); Values.put (MediaStore.Files.FileColumns.FORMAT, mtpconstants.format_association); Values.put (MediaStore.Files.FileColumns.DATA, folderpath); Values.put (MediaStore.Files.FileColumns.DATE_MODIFIED, folderstructure.mlastmodified); Values.put (MediaStore.Files.FileColumns.SIZE, 0); Values.put (MediaStore.Files.FileColumns.IS_DRM, 0);Folderlist.add (values); If (folderlist.size () >=) {sortbypath (folderlist); Flush (mediascannerinserter.file_uri, folderlist); Folderlist.clear (); }}} if (!folderlist.isempty ()) {sortbypath (folderlist); Flush (mediascannerinserter.file_uri, folderlist); }///Insert task finish, Execute single type scan Task. Executesingletypescantask (); Long end = System.currenttimemillis (); MTKLOG.V (TAG, "Insert All folder entries Finsih in" + thread.currentthread (). getName () + ": folder size =" + mfoldermap.size () + ", Insert num =" + folderlist.size () + ", cost =" + (end-start) + "ms"); private void Sortbypath (list<contentvalues> list) {collections.sort (list, New Comparator<con Tentvalues> () { public int Compare (contentvalues old, contentvalues latest) {String oldpath = old.getasstring (Me diaStore.Files.FileColumns.DATA); String Latestpath = latest.getasstring (MediaStore.Files.FileColumns.DATA); If (latestpath! = null && oldpath! = Null) {return oldpath.compareto (latestpath); } return 0; }; }); Private Boolean isexistindatabase (String folderpath) {cursor cursor = null; Try {cursor = mcontentresolver.query (mediascannerinserter.file_uri, null, M EdiaStore.Files.FileColumns.DATA + "=?", New string[] {folderpath}, null, null); return cursor! = null && cursor.movetofirst (); } catch (Exception e) {mtklog.e (TAG, "Check isexistindatabase with Exception for" + folderpath, e); RetUrn true; } finally {if (cursor! = Null) {cursor.close (); }}} private void Flush (Uri tableuri, list<contentvalues> List) {long start = System.currenttimemillis (); int size = List.size (); contentvalues[] Valuesarray = new contentvalues[size]; Valuesarray = List.toarray (valuesarray); Try {mcontentresolver.bulkinsert (tableuri, valuesarray); } catch (Exception E) {mtklog.e (TAG, "bulkinsert with Exception for" + tableuri, e); } Long end = System.currenttimemillis (); MTKLOG.D (TAG, "flush" + Tableuri + "with size" + size + "which cost" + (end-start) + "ms"); } }</pre>Add//<2015.05.06> Modify for Start<br><2015.05.06> Modify for End<p><p></p></p><p><p>The modification<br><br></p></p><p><p>Android Mediaprovider--file management: must be present, File Manager in a few new folders, after bulk deletion, connected to the computer to see still some folders are not deleted</p></p></span>

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.