Back to project page Avatar.
The source code is released under:
GNU General Public License
If you think the Android project Avatar listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.syw.avatar; /*from w w w . j a v a 2 s.c om*/ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.os.AsyncTask; import android.os.Bundle; import android.provider.MediaStore; import android.provider.MediaStore.Images.Thumbnails; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.Window; import android.widget.TextView; import com.syw.avatar.AlbumFragment.OnAlbumClickedListener; import com.syw.avatar.PhotoPickerFragment.OnPhotoSelectClickListener; import com.syw.avatar.util.ThumbnailsUtil; public class LocalPhotoActivity extends FragmentActivity implements OnAlbumClickedListener ,OnPhotoSelectClickListener{ @SuppressWarnings("unused") private static final String TAG = LocalPhotoActivity.class.getSimpleName(); private TextView titleTextView; private TextView tvLeftArrowBtn; private TextView tvRightCancelBtn; private AlbumFragment photoFolderFragment; private PhotoPickerFragment photoPickerFragment; private Fragment currentFragment; private FragmentManager manager; private List<AlbumInfo> listImageInfo = new ArrayList<AlbumInfo>(); private ContentResolver cr; private String albumName = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_localphoto); titleTextView = (TextView) findViewById(R.id.tvTitleName); titleTextView.setText("????"); tvLeftArrowBtn = (TextView) findViewById(R.id.tvTitleArrowBtnLeft); tvLeftArrowBtn.setText("??"); tvLeftArrowBtn.setVisibility(View.GONE); tvLeftArrowBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ?? "< ??"??, ???????????fragment; showPhotoAlbumFragment(); } }); tvRightCancelBtn = (TextView) findViewById(R.id.tvTitleBtnRightButton); tvRightCancelBtn.setText(getString(R.string.cancel)); tvRightCancelBtn.setVisibility(View.VISIBLE); tvRightCancelBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setResult(RESULT_CANCELED); LocalPhotoActivity.this.finish(); } }); manager = getSupportFragmentManager(); photoFolderFragment = new AlbumFragment(); photoPickerFragment = new PhotoPickerFragment(); cr = getContentResolver(); listImageInfo.clear(); albumName = getIntent().getStringExtra("albumName"); new ImageLoadAsyncTask().execute(); } // ????????????????????????? ; @Override public void onAlbumClickedListener(String albumName, List<PhotoInfo> list) { // ????????Fragment; showPhotoPickerFragment(albumName, list); } // ????????????Fragment; private void showPhotoPickerFragment(String albumName, List<PhotoInfo> list) { // ??????; tvLeftArrowBtn.setVisibility(View.VISIBLE); titleTextView.setText(albumName); FragmentTransaction transaction = manager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); // ????????Fragment; if (!photoPickerFragment.isAdded()) { // ????????add?,????add?,??add??fragment; Bundle args = new Bundle(); // ??????????????????????????photopickfragment; PhotoSerializable photoSerializable = new PhotoSerializable(); photoSerializable.setList(list); args.putSerializable("list", photoSerializable); photoPickerFragment.setArguments(args); transaction.hide(photoFolderFragment).add(R.id.fragment_container, photoPickerFragment).commit(); // ????????fragment?add????Activity? } else { // ?????add?,??????fragment,???????fragment; photoPickerFragment.updateDataList(list); transaction.hide(photoFolderFragment).show(photoPickerFragment).commit(); // ????????fragment?????? } currentFragment = photoPickerFragment; } // ??????????; private void showPhotoAlbumFragment() { // ????????; tvLeftArrowBtn.setVisibility(View.GONE); titleTextView.setText("????"); FragmentTransaction transaction = manager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); if (!photoFolderFragment.isAdded()){ transaction.hide(photoPickerFragment).add(R.id.fragment_container, photoFolderFragment).commit(); } else{ transaction.hide(photoPickerFragment).show( photoFolderFragment).commit(); } currentFragment = photoFolderFragment; } /* * ???????????????????????; * fragment?????????????; */ @Override public void onOKClickListener(PhotoInfo selectedPhoto) { Intent data = new Intent(); data.putExtra("photoPath", selectedPhoto.getPathAbsolute()); data.putExtra("albumName", titleTextView.getText()); // ????album????????; setResult(RESULT_OK, data); finish(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_CANCELED){ // ???????caceled,?????????????activity; finish(); return; } // ????????????????????????? switch (requestCode) { case 50001: break; default: break; } } @Override public void onBackPressed() { // ???????????????????,????activity; if (currentFragment == photoFolderFragment){ setResult(RESULT_CANCELED); finish(); } else{ // ????????????????,???????????; ????????Fragment; showPhotoAlbumFragment(); } } @Override public void onPause(){ super.onPause(); } @Override public void finish() { super.finish(); this.overridePendingTransition(R.anim.activity_stay, R.anim.photo_picker_activity_close); } private class ImageLoadAsyncTask extends AsyncTask<Void, Void, Object>{ @Override protected Object doInBackground(Void... params) { //??????? ThumbnailsUtil.clear(); String[] projection = { Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails.DATA }; Cursor cur = cr.query(Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null); if (cur!=null&&cur.moveToFirst()) { int image_id; String image_path; int image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID); int dataColumn = cur.getColumnIndex(Thumbnails.DATA); do { image_id = cur.getInt(image_idColumn); image_path = cur.getString(dataColumn); ThumbnailsUtil.put(image_id, "file://"+image_path); } while (cur.moveToNext()); } cur.close(); //?????? Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, "date_modified DESC"); String _path="_data"; String _album="bucket_display_name"; HashMap<String,AlbumInfo> myhash = new HashMap<String, AlbumInfo>(); AlbumInfo albumInfo = null; PhotoInfo photo = null; if (cursor!=null&&cursor.moveToFirst()) { do{ int index = 0; int _id = cursor.getInt(cursor.getColumnIndex("_id")); String path = cursor.getString(cursor.getColumnIndex(_path)); String album = cursor.getString(cursor.getColumnIndex(_album)); List<PhotoInfo> stringList = new ArrayList<PhotoInfo>(); photo = new PhotoInfo(); if(myhash.containsKey(album)){ albumInfo = myhash.remove(album); if(listImageInfo.contains(albumInfo)) index = listImageInfo.indexOf(albumInfo); photo.setImageId(_id); photo.setPathFile("file://"+path); photo.setPathAbsolute(path); albumInfo.getList().add(photo); listImageInfo.set(index, albumInfo); myhash.put(album, albumInfo); }else{ albumInfo = new AlbumInfo(); stringList.clear(); photo.setImageId(_id); photo.setPathFile("file://"+path); photo.setPathAbsolute(path); stringList.add(photo); albumInfo.setImage_id(_id); albumInfo.setPath_file("file://"+path); albumInfo.setPath_absolute(path); albumInfo.setName_album(album); albumInfo.setList(stringList); listImageInfo.add(albumInfo); myhash.put(album, albumInfo); } }while (cursor.moveToNext()); cursor.close(); } return null; } @Override protected void onPostExecute(Object result) { super.onPostExecute(result); List<PhotoInfo> listInit = null; if (albumName != null && albumName.length() > 0){ for (AlbumInfo ai : listImageInfo){ if (ai.getName_album().equals(albumName)){ listInit = ai.getList(); break; } } } Bundle args = new Bundle(); AlbumSerializable photoSerializable = new AlbumSerializable(); photoSerializable.setList(listImageInfo); args.putSerializable("list", photoSerializable); photoFolderFragment.setArguments(args); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.fragment_container, photoFolderFragment); transaction.commit(); currentFragment = photoFolderFragment; if (listInit != null){ showPhotoPickerFragment(albumName, listInit); } } } }