android选取照片后做处理
2016-11-24 09:34
169 查看
1.
携带参数选取照片,调用下列方法可以进入照片的处理
最后得到照片结果的处理
携带参数选取照片,调用下列方法可以进入照片的处理
public static void takePictureFromAlbum(Activity activity, int maxCount, List<PhotoInfo> selectPhotoList) { Intent imageIntent = new Intent(activity, SelectPhotoActivity.class); PhotoSerializable photoSerializable = new PhotoSerializable(); photoSerializable.setList(selectPhotoList); Bundle bundle = new Bundle(); bundle.putSerializable("photoSerializable", photoSerializable); imageIntent.putExtra("max", maxCount); imageIntent.putExtras(bundle); activity.startActivityForResult(imageIntent, REQUEST_CODE_PICKER_ABULM); }
选取照片的activity,一下activity是用于选择照片并存入参数list的容器中
public class SelectPhotoActivity extends BaseActivity implements PhotoFolderFragment.OnPageLodingClickListener, PhotoFragment.OnPhotoSelectClickListener { private PhotoFolderFragment photoFolderFragment; private List<PhotoInfo> selectPhoto; private FragmentManager manager; private int backInt = 0; private int maxSelectImageCount = 9; /** * 已选择图片数量 */ @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_select_photo); PhotoSerializable photoSerializable = (PhotoSerializable) getIntent() .getExtras().getSerializable("photoSerializable") ; maxSelectImageCount = getIntent().getIntExtra("max", 9); selectPhoto = photoSerializable.getList() ; initToolBar() ; manager = getSupportFragmentManager(); photoFolderFragment = new PhotoFolderFragment(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.body,photoFolderFragment); transaction.addToBackStack(null); transaction.commit(); } private void initToolBar() { toolbarTitle.setVisibility(View.VISIBLE); toolbarTitle.setText("一共选择了" + selectPhoto.size() + "张"); toorbar_back.setVisibility(View.VISIBLE); toorbar_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(backInt==0){ finish(); }else if(backInt==1){ backEvent(); } } }); oprateText.setVisibility(View.VISIBLE); oprateText.setText("完成"); oprateText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(selectPhoto.size()>0){ Intent intent = new Intent(); Bundle bundle = new Bundle() ; PhotoSerializable photoSerializable = new PhotoSerializable(); photoSerializable.setList(selectPhoto) ; bundle.putSerializable("photoSerializable", ph 128fc otoSerializable); // 设置返回数据 intent.putExtras(bundle) ; setResult(RESULT_OK, intent); finish(); }else{ Toast.makeText(SelectPhotoActivity.this, "至少选择一张图片", Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onStart() { super.onStart(); } @Override public void onPageLodingClickListener(List<PhotoInfo> list) { XDLog.e("SelectPhotoActivity","onPageLodingClickListener"); FragmentTransaction transaction = manager.beginTransaction(); PhotoFragment photoFragment = new PhotoFragment(); Bundle bundle = new Bundle(); PhotoSerializable photoSerializable = new PhotoSerializable(); for (PhotoInfo photoInfoBean : list) { if(selectPhoto.contains(photoInfoBean)){ photoInfoBean.setChoose(true); }else{ photoInfoBean.setChoose(false) ; } } photoSerializable.setList(list); bundle.putSerializable("list", photoSerializable); bundle.putInt("selectcount", selectPhoto.size()) ; photoFragment.setArguments(bundle); transaction = manager.beginTransaction(); transaction.replace(R.id.body,photoFragment); transaction.addToBackStack(null); transaction.commit(); backInt++; } @Override public void onPhotoSelectClickListener(List<PhotoInfo> list) { XDLog.e("SelectPhotoActivity","onPhotoSelectClickListener"); for (PhotoInfo photoInfoBean : list) { if(photoInfoBean.isChoose()){ if(!selectPhoto.contains(photoInfoBean)){ selectPhoto.add(photoInfoBean); } }else{ if(selectPhoto.contains(photoInfoBean)){ selectPhoto.remove(photoInfoBean) ; } } } toolbarTitle.setText("已选择" + selectPhoto.size() +"/"+maxSelectImageCount +"张"); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode == KeyEvent.KEYCODE_BACK&&backInt==0){ finish(); }else if(keyCode == KeyEvent.KEYCODE_BACK&&backInt==1){ backEvent(); } return false; } public int getMaxSelectImageCount() { return maxSelectImageCount; } private void backEvent(){ backInt--; toolbarTitle.setText("已选择" + selectPhoto.size()+"/"+maxSelectImageCount +"张"); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.body,photoFolderFragment); manager.popBackStack(0, 0); } }
public class BaseActivity extends AppCompatActivity { private ToolBarHelper mToolBarHelper; public Toolbar toolbar; public TextView toolbarQuit, toolbarTitle, oprateText, toolbarTitleArrow; public ImageView operateImg; public FrameLayout toolbar_container; public Toolbar id_tool_bar; public ImageView toorbar_back; public RadioButton leftRadioGroup; public RadioButton rightRadioGroup; public TextView base_redpoint; public TabLayout tabs; public CircleImageView circleImageView; protected final static String TAG = BaseActivity.class.getName(); protected Subscription subscription; protected RxBus rxBus; /** * Notification管理 */ public NotificationManager mNotificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); ActivityHelper.getInstance().addActivity(this); rxBus = RxBus.getDefault(); initService(); } /* * 添加事件监听器 子类在需要用到事件通知时调用此方法即可 * 该方法需要与busEventHandler一起使用 */ protected void configureRxBus() { subscription = rxBus.toObserverable(RxBusEvent.class).subscribe(rxBusEvent -> { busEventHandler(rxBusEvent); }); } /** * 初始化要用到的系统服务 */ private void initService() { mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } /** * 清除当前创建的通知栏 */ public void clearNotify(int notifyId) { mNotificationManager.cancel(notifyId);//删除一个特定的通知ID对应的通知 // mNotification.cancel(getResources().getString(R.string.app_name)); } /** * 清除所有通知栏 */ public void clearAllNotify() { mNotificationManager.cancelAll();// 删除你发的所有通知 } /** * 事件处理 在接收到RxBus事件后会调用此方法来处理事件 * * @param event 事件类型 */ protected void busEventHandler(RxBusEvent event) { } protected void sendRxBuxEvent(RxBusEvent event) { rxBus.post(event); } @Override public void setContentView(int layoutResID) { mToolBarHelper = new ToolBarHelper(this, layoutResID); toolbar = mToolBarHelper.getToolBar(); setContentView(mToolBarHelper.getContentView()); /*把 toolbar 设置到Activity 中*/ setSupportActionBar(toolbar); /*自定义的一些操作*/ onCreateCustomToolBar(toolbar); File destDir = new File(Constant.Cache_Path); if (!destDir.exists()) { destDir.mkdirs(); } } @Override protected void onResume() { super.onResume(); JPushInterface.onResume(this); if (!isContainFragment()) { MobclickAgent.onPageStart(getClass().getName()); //统计页面(仅有Activity的应用中SDK自动调用,不需要单独写。) } MobclickAgent.onResume(this); //统计时长 HashMap<String, String> map = new HashMap<String, String>(); map.put("name", getClass().getName()); UMengSDKHelper.reportEventWithParams(this, UMengSDKHelper.EventID.VISIT_PAGE, map); } @Override protected void onPause() { super.onPause(); JPushInterface.onPause(this); if (!isContainFragment()) { MobclickAgent.onPageEnd(getClass().getName()); // (仅有Activity的应用中SDK自动调用,不需要单独写)保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息。 } MobclickAgent.onPause(this); } protected boolean isContainFragment() { return false; } @Override protected void onDestroy() { super.onDestroy(); if (subscription != null && !subscription.isUnsubscribed()) { subscription.unsubscribe(); } } public void onCreateCustomToolBar(Toolbar toolbar) { toolbar.setContentInsetsRelative(0, 0); circleImageView = (CircleImageView) toolbar.findViewById(R.id.classcircle_civ_avatar); toorbar_back = (ImageView) toolbar.findViewById(R.id.toorbar_back); toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); toolbarTitleArrow = (TextView) toolbar.findViewById(R.id.toolbar_title_arrow); operateImg = (ImageView) toolbar.findViewById(R.id.operate_img); oprateText = (TextView) toolbar.findViewById(R.id.operate_text); toolbarQuit = (TextView) toolbar.findViewById(R.id.toolbar_quit); id_tool_bar = (Toolbar) toolbar.findViewById(R.id.id_tool_bar); toolbar_container = (FrameLayout) toolbar.findViewById(R.id.toolbar_container); base_redpoint = (TextView) toolbar.findViewById(R.id.base_redpoint); tabs = (TabLayout) toolbar.findViewById(R.id.tabs); } protected void showLoading(String content) { AlertDialogHelper.showLoadingDialog(this, content); } protected void showSuccessAlert(String content) { AlertDialogHelper.showSuccessDialog(this, content); } public RadioButton getLeftRadioGroup() { return leftRadioGroup; } public void showErrorAlert(String errorMsg) { AlertDialogHelper.showErrorDialog(this, errorMsg); } protected void dismissAlert() { AlertDialogHelper.dismissDialog(); } protected void finishAllActivity() { ActivityHelper.getInstance().finishAllActivity(); } /** * @获取默认的pendingIntent,为了防止2.3及以下版本报错 * @flags属性: 在顶部常驻:Notification.FLAG_ONGOING_EVENT * 点击去除: Notification.FLAG_AUTO_CANCEL */ public PendingIntent getDefalutIntent(int flags) { PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(), flags); return pendingIntent; } }
在activity中布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" > <FrameLayout android:id="@+id/body" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </RelativeLayout>
activity中的fragment
public class PhotoFolderFragment extends BaseFragment { public interface OnPageLodingClickListener { public void onPageLodingClickListener(List<PhotoInfo> list); } private OnPageLodingClickListener onPageLodingClickListener; private GridView gridview; private ContentResolver cr; private List<AlbumInfo> listImageInfo = new ArrayList<AlbumInfo>(); private PhotoFolderAdapter listAdapter; private LinearLayout loadingLay; @Override public void onAttach(Activity activity) { super.onAttach(activity); if(onPageLodingClickListener==null){ onPageLodingClickListener = (OnPageLodingClickListener)activity; } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_photofolder, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); gridview = (GridView)getView().findViewById(R.id.gridview); loadingLay = (LinearLayout)getView().findViewById(R.id.loadingLay); cr = getActivity().getContentResolver(); listImageInfo.clear(); new ImageAsyncTask().execute(); gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { onPageLodingClickListener.onPageLodingClickListener(listImageInfo.get(position).getList()); } }); } private class ImageAsyncTask 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); if (FileUtils.isFileExist(image_path)){ ThumbnailsUtil.put(image_id,image_path); } } while (cur.moveToNext()); } //获取原图 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>(); 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)); if (!FileUtils.isFileExist(path))continue; List<PhotoInfo> stringList = new ArrayList<PhotoInfo>(); AlbumInfo albumInfo = null; PhotoInfo photoInfo = new PhotoInfo(); if(myhash.containsKey(album)){ albumInfo = myhash.remove(album); if(listImageInfo.contains(albumInfo)) index = listImageInfo.indexOf(albumInfo); photoInfo.setImage_id(_id); photoInfo.setPath_file("file://"+path); photoInfo.setPath_absolute(path); albumInfo.getList().add(photoInfo); listImageInfo.set(index, albumInfo); myhash.put(album, albumInfo); }else{ albumInfo = new AlbumInfo(); stringList.clear(); photoInfo.setImage_id(_id); photoInfo.setPath_file("file://"+path); photoInfo.setPath_absolute(path); stringList.add(photoInfo); 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()); } return null; } @Override protected void onPostExecute(Object result) { super.onPostExecute(result); loadingLay.setVisibility(View.GONE); if(getActivity()!=null){ listAdapter = new PhotoFolderAdapter(getActivity(), listImageInfo); gridview.setAdapter(listAdapter); } } } }
fragment的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/loadingLay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center_horizontal" android:orientation="vertical" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="相册加载中..." android:textSize="18sp" /> </LinearLayout> <GridView android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:listSelector="@null" android:numColumns="2" android:horizontalSpacing="5dip" android:verticalSpacing="5dip" android:layout_margin="5dip" android:stretchMode="columnWidth" /> </RelativeLayout>
最后得到照片结果的处理
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { Uri avatarUri = Uri.fromFile(new File(viewModel.getAvatarPath())); DisplayMetrics display = this.getResources().getDisplayMetrics(); int width = display.widthPixels; int height = display.heightPixels; if (requestCode == ImagePickerUtils.REQUEST_CODE_PICKER_ABULM) { //从相册中选取 if (data == null) return; Bundle bundle = data.getExtras(); if (bundle != null) { PhotoSerializable photoSerializable = (PhotoSerializable) bundle.getSerializable("photoSerializable"); List<PhotoInfo> selectPhotos = photoSerializable.getList(); PhotoInfo photoInfo = selectPhotos.get(0); Crop.of(Uri.fromFile(new File(photoInfo.getPath_absolute())), avatarUri) .asSquare() .withMaxSize(width, height) .start(this); } } } }
相关文章推荐
- Androd选取相册照片和拍照处理-android学习之旅(62)
- Androd选取相册照片和拍照处理-android学习之旅(62)
- Androd选取相册照片和拍照处理-android学习之旅(62)
- Androd选取相册照片和拍照处理-android学习之旅(62)
- android 上图片选取器以及图片尺寸太大情况下处理
- Android 选取照片
- Android 实现调用照相机和选取本地照片功能
- Android--从相册中选取照片并返回结果
- 给力的android手机照片处理软件(PicClipper)诞生记
- Android:处理调用系统相机照片被压缩问题
- android拍照或相册选照片,裁剪,圆图处理(4.4变动后)
- Android处理照片质量、分辨率、旋转角度一些方法
- Android照片处理——涂鸦和拼图实现
- Android:处理调用系统相机照片被压缩问题
- Android ColorMatrix类图像颜色处理-黑白老照片、泛黄旧照片、高对比度等效果
- 图片处理系列二Android瀑布流照片墙实现(含源码)
- android菜瓜笔记之手机照片选取与拍摄
- Android中照相,从相册选取照片,压缩,保存到手机内存,展示到界面,点击放大,上传。(含demo)
- Android调用相册或相机拍照选取照片并裁剪