Android high imitation WeChat photo taking, multiple choice, preview, delete (remove photos) album Function

Source: Internet
Author: User

Android high-imitation photos, multiple choice, preview, and delete (remove photos) album functions

First, it is better to declare that it is better to teach people and fish than to teach people and fish. Only one idea can be provided. Of course, if you need the source code, you can ask me the source code privately for a fee: QQ: 508181017.

 

 

After nearly three years of work, I have never been able to take photos of my own research system. Here, I came back with a high-imitation photo album function, multiple choice, preview, and delete (remove photos, all previously developed applications have this requirement, but they have never been practical! 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 the custom path and display the photos in different folders.

3. Specify multiple images.

4. The collections used include:

(1) All photo Sets

(2) Photo sets under different files

(3) folder set for saving 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 album set so that checkbox is checked.

6. Problems Encountered by myself include:

(1) setOnCheckedChangeListen of the CheckBox when the selected number of photos exceeds the specified numberEr events may encounter errors (not a bug, maybe my code logic is not rigorous enough), mainly when setting the setChecked (false | true) method that comes with the control; so I used the onClick method to achieve the selected state.

(2) preview the album and return to the upper-level (display the photos in the folder). Considering the performance, I used the map set to record and save the selected photos, when you call back the page, you can traverse the photos in the current folder to check whether the map set is included. If yes, 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 unselect the image and callback to the interface to display the desired effect.

7. Main Code:

(1) obtain the 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 albums

 

 

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 photo", 0, cursor. getString (cursor. getColumnIndex (ImageColumns. DATA), true); // "recent photo" album 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 photos in the corresponding album.

 

 

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 album 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 directory found", Toast. LENGTH_LONG ). show ();}}
(5) Take a photo and update the local album 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 Log is available for sd. 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 ();}}


 

 

 

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.