Android high-profile photo taking, multiple choice, preview, and delete (photo removal) photo sharing functions
I have been working for nearly three years, and I have never been involved in the camera and camera of the study system. Here I came back with a high imitation photo, multiple selections, preview, and delete (remove photos) phase functions,
All previously developed applications have this requirement, but it has never been useful! I will not talk much about it. Let's take a look at the following ideas:
1. photos can be saved locally and queried in real time (broadcast or service is not required)
2. Save the photo to a custom path and display the photos in different directories.
3. Specify multiple images.
4. The collections used include:
(1) All photo Sets
(2) Photo sets under different files
(3) directory set for storing photos by the System
(4) selected photo Sets
5. Each time you select and return to the upper-level interface, you must pass the selected photos to compare them with the currently displayed list so that the checkbox is checked.
6. Problems Encountered by myself include:
(1) setOnCheckedChangeListen of the CheckBox when the selected number of photos exceeds the specified numberAn error occurs in the er event (not a bug, maybe my code logic is not rigorous enough), mainly when you set the setChecked (false | true) method that comes with the control.
So I used the onClick method to achieve the selected state.
(2) preview the comparison of the set when the superior (the photo under the display directory) is returned. Considering the performance, I used a map set to record and save the selected photos.
When the callback interface is used, the photos in the current directory are traversed to determine whether the map set is included, including whether the checkbox is selected.
(3) When you delete an image, your business is not the same. This project only allows the user to select and callback to the interface is the same as the unselected and can display the desired effect.
7. Main Code:
(1) obtain a list of recent photos
public List
getCurrent() {Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns.DATA,ImageColumns.DATE_ADDED, ImageColumns.SIZE }, null, null, ImageColumns.DATE_ADDED);if (cursor == null || !cursor.moveToNext())return new ArrayList
();List
photos = new ArrayList
();cursor.moveToLast();do {if (cursor.getLong(cursor.getColumnIndex(ImageColumns.SIZE)) > 1024 * 10) {PhotoModel photoModel = new PhotoModel();photoModel.setOriginalPath(cursor.getString(cursor.getColumnIndex(ImageColumns.DATA)));photos.add(photoModel);}} while (cursor.moveToPrevious());return photos;}
(2) obtain the list of all phases
Public List getAlbums () {List albums = new ArrayList (); Map
Map = new HashMap
(); Cursor cursor = resolver. query (Media. EXTERNAL_CONTENT_URI, new String [] {ImageColumns. DATA, ImageColumns. BUCKET_DISPLAY_NAME, ImageColumns. SIZE}, null); if (cursor = null |! Cursor. moveToNext () return new ArrayList (); cursor. moveToLast (); AlbumModel current = new AlbumModel ("recent photos", 0,
Cursor. getString (cursor. getColumnIndex (ImageColumns. DATA), true); // "recent photos" are similar to albums. add (current); do {if (cursor. getInt (cursor. getColumnIndex (ImageColumns. SIZE) <1024*10) continue; current. increaseCount (); String name = cursor. getString (cursor. getColumnIndex (ImageColumns. BUCKET_DISPLAY_NAME); if (map. keySet (). contains (name) map. get (name ). increaseCount (); else {AlbumModel album = new AlbumModel (name, 1, cursor. getString (cursor. getColumnIndex (ImageColumns. DATA); map. put (name, album); albums. add (album) ;}}while (cursor. moveToPrevious (); return albums ;}
(3) obtain the corresponding photo
public List
getAlbum(String name) {Cursor cursor = resolver.query(Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns.BUCKET_DISPLAY_NAME,ImageColumns.DATA, ImageColumns.DATE_ADDED, ImageColumns.SIZE }, "bucket_display_name = ?",new String[] { name }, ImageColumns.DATE_ADDED);if (cursor == null || !cursor.moveToNext())return new ArrayList
();List
photos = new ArrayList
();cursor.moveToLast();do {if (cursor.getLong(cursor.getColumnIndex(ImageColumns.SIZE)) > 1024 * 10) {PhotoModel photoModel = new PhotoModel();photoModel.setOriginalPath(cursor.getString(cursor.getColumnIndex(ImageColumns.DATA)));photos.add(photoModel);}} while (cursor.moveToPrevious());return photos;}
(4) Take a photo and update the local photo in time
Public void photo () {// Intent openCameraIntent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // startActivityForResult (openCameraIntent, TAKE_PICTURE); try {File dir = new File (Environment. getExternalStorageDirectory () + "/" + localTempImgDir); System. out. println ("image name:" + dir. getPath (); if (! Dir. exists () {dir. mkdirs ();} localTempImgFileName = System. currentTimeMillis () + ". jpg "; Intent intent = new Intent (android. provider. mediaStore. ACTION_IMAGE_CAPTURE); File f = new File (dir, localTempImgFileName); // localTempImgDir and localTempImageFileName are custom names Uri u = Uri. fromFile (f); intent. putExtra (MediaStore. images. media. ORIENTATION, 0); intent. putExtra (MediaStore. EXTRA_OUTPUT, u); startActivityForResult (intent, ResultTag. CODE_TOPHOTO);} catch (ActivityNotFoundException e) {Toast. makeText (PublishActivity. this, "No storage folder found", Toast. LENGTH_LONG ). show ();}}
(5) Take a photo and update the local photo in time
@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); // The camera returns if (requestCode = ResultTag. CODE_TOPHOTO) {File f = new File (Environment. getExternalStorageDirectory () + "/" + localTempImgDir + "/" + localTempImgFileName); String sdStatus = Environment. getExternalStorageState (); if (! SdStatus. equals (Environment. MEDIA_MOUNTED) {// check whether the specified sd is available in Log. v ("TestFile", "SD card is not avaiable/writeable right now. "); return;} try {Uri u = Uri. parse (android. provider. mediaStore. images. media. insertImage (getContentResolver (), f. getAbsolutePath (), null, null); System. out. println ("Address:" + f. getAbsolutePath (); MediaScannerConnection. scanFile (this, new String [] {f. getAbsolutePath ()}, null, new MediaScannerConnection. onScanCompletedListener () {public void onScanCompleted (String path, Uri uri) {Log. I ("ExternalStorage", "Scanned" + path + ":"); Log. I ("ExternalStorage", "-> uri =" + uri) ;}});} catch (FileNotFoundException e) {e. printStackTrace ();} PhotoModel takePhoto = new PhotoModel (); takePhoto. setChecked (true); takePhoto. setOriginalPath (f. getAbsolutePath (); selectedShow. add (takePhoto); adapter. notifyDataSetChanged ();}}