As a result of recent job requirements, one of the functions is to save the photos inside the webview locally, and to have a timely feedback directly in the album.
In the project, the implementation process is such a design WebView Click Interactive Event, when you click on the image in the webview to grab the URL address of the image, when you need to save the picture, download the picture by Nohttp and save the picture to local, and then update the album.
Here, crawl URL address and download pictures of the scheme there are many, here is not to repeat, here is to explain how to update the album, because here is a pit, I have encountered a total of two scenarios:
Programme one:
Send a broadcast notification system to update the album.
This is done most of the time online, why? Because it's simple, just send a broadcast to OK. But this method is a pit, where the pit is, one will say, here first how to achieve:
New Intent (intent.action_media_scanner_scan_file, uri.fromfile(new FILE (url.webviewimagepath+ "/image.jpg"))); File File== uri.fromfile (file); Intent.setdata (Uri); Showimageactivity. this. Sendbroadcast (intent);
The above code is the way to send the broadcast, where url.webviewimagepath+ "/image.jpg" is saved to the local address and picture format, file is to download the object of the image to file. Then send the broadcast notification system to update the album just fine.
So now the question is, where is the pit. Here are two main questions:
1, album update is very slow
This is because we send a broadcast notification to the system, but cannot tell the system to refresh a particular file, so the system refreshes the entire SD card. As a result, natural album updates will be slow.
2, some of the Android version of the album Can not be updated, restart the phone will be updated
This is because in part of the Android version, considering the problem of system security, is not allowed to send the app broadcast to the system, so naturally unable to update the album, and another aspect, the phone restarts, will re-mount the SD card, which will refresh the SD to open everything.
So the question is, since there are so many pits on the way to refresh the album by radio, is there any good way? This is the second way:
Scenario Two:
Using Mediascanner
What is Mediascanner? Mediascanner completes the scanning of multimedia files in Android. For example, Mediascanner scans the system memory and SD card files, the results of the scan will be loaded in the database, the music in this application to see the list of the activity of the song album name, genre, song length and other information, Are all the information in the database that was put in the database after the scan and the last read. One thing to understand here is that the information in the multimedia database is added by Mediascanner. Here's how to use it:
classSannerclientImplementsmediascannerconnection.mediascannerconnectionclient { Public voidonmediascannerconnected () {if(Mfile = =NULL) { return; } scan (Mfile, mmimetype); } Public voidonscancompleted (String path, Uri uri) {mconn.disconnect (); } Private voidScan (file file, String type) {if(File.isfile ()) {mconn.scanfile (), File.getabsolutepath (),NULL); return; } //This method can traverse multiple files, and there is no need tofile[] Files =File.listfiles (); if(Files = =NULL) { return; } for(File f:file.listfiles ()) {Scan (f, type); } } } Public voidscanfile (file file, String mimeType) {mfile=file; Mmimetype=MimeType; Mconn.connect (); }
In the place where we need to add photos to the album, these calls to Scanfile are available, with two parameters: file refers to the files that are added, MimeType is the format of the scanned file. This allows us to directly scan for adding specific files. Increase the speed
Some things Android saves photos to albums