以下是选择照片的界面:
package yedashen.voiceapp.Module.WorkTask;import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;import com.bumptech.glide.Glide;import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;import yedashen.voiceapp.R;
import yedashen.voiceapp.Utils.ToastUtils;
import yedashen.voiceapp.Utils.WindowUtil;public class SelectPictureActivity extends Activity {private static final int TAKE_PICTURE = 520;public static final String INTENT_MAX_NUM = "intent_max_num";public static final String INTENT_SELECTED_PICTURE = "intent_selected_picture";private Context context;private GridView gridview;private PictureAdapter adapter;private ContentResolver mContentResolver;private ArrayList<String> imagePathList = new ArrayList<String>();/*** 已选择的图片*/private ArrayList<String> selectedPicture = new ArrayList<String>();private ArrayList<String> selected = new ArrayList<String>();private String cameraPath = null;private TextView tv_wc;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_select_picture);WindowUtil.setTraspartenStatusBar(this);selected = (ArrayList<String>) getIntent().getSerializableExtra("selected");context = this;mContentResolver = getContentResolver();initView();}private void initView() {tv_wc = (TextView) findViewById(R.id.tv_wc);ImageView iv_gzrw_back = (ImageView) findViewById(R.id.iv_gzrw_back);iv_gzrw_back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {finish();}});tv_wc.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (selectedPicture.size() > 1) {ToastUtils.showToast("只能选一张照片亲");return;}Intent data = new Intent();data.putExtra(INTENT_SELECTED_PICTURE, selectedPicture);setResult(RESULT_OK, data);finish();}});gridview = (GridView) findViewById(R.id.gridview);adapter = new PictureAdapter();gridview.setAdapter(adapter);gridview.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {if (position == 0) {if (ContextCompat.checkSelfPermission(SelectPictureActivity.this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(SelectPictureActivity.this,new String[]{"Manifest.permission.CAMERA"}, 0);} else {goCamare();}}}});getThumbnail();}/*** 使用相机拍照** @version 1.0* @author zyh*/protected void goCamare() {Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);ContentValues contentValues = new ContentValues(1);contentValues.put(MediaStore.Images.Media.DATA, getOutputMediaFile().getAbsolutePath());Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);startActivityForResult(openCameraIntent, TAKE_PICTURE);}/*** 用于拍照时获取输出的Uri** @return* @version 1.0* @author zyh*/protected Uri getOutputMediaFileUri() {File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Night");if (!mediaStorageDir.exists()) {if (!mediaStorageDir.mkdirs()) {Log.d("MyCameraApp", "failed to create directory");return null;}}String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");cameraPath = mediaFile.getAbsolutePath();return Uri.fromFile(mediaFile);}protected File getOutputMediaFile() {File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Night");if (!mediaStorageDir.exists()) {if (!mediaStorageDir.mkdirs()) {Log.d("MyCameraApp", "failed to create directory");return null;}}String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");cameraPath = mediaFile.getAbsolutePath();return mediaFile;}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK && cameraPath != null) {selectedPicture.add(cameraPath);Intent data2 = new Intent();data2.putExtra(INTENT_SELECTED_PICTURE, selectedPicture);setResult(RESULT_OK, data2);this.finish();}}class PictureAdapter extends BaseAdapter {@Overridepublic int getCount() {return imagePathList.size() + 1;}@Overridepublic Object getItem(int position) {return null;}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = null;if (convertView == null) {convertView = View.inflate(context, R.layout.item_gridview_picture, null);holder = new ViewHolder();holder.iv = (ImageView) convertView.findViewById(R.id.iv);holder.checkBox = (Button) convertView.findViewById(R.id.check);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}if (position == 0) {holder.iv.setImageResource(R.mipmap.pickphotos_to_camera_normal);holder.checkBox.setVisibility(View.INVISIBLE);} else {position = position - 1;holder.checkBox.setVisibility(View.VISIBLE);final String imagePath = imagePathList.get(position);Glide.with(context).load("file://" + imagePath).into(holder.iv);boolean isSelected = selectedPicture.contains(imagePath);holder.checkBox.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (selectedPicture.contains(imagePath)) {selectedPicture.remove(imagePath);} else {selectedPicture.add(imagePath);}tv_wc.setEnabled(selectedPicture.size() > 0);tv_wc.setText("完成");v.setSelected(selectedPicture.contains(imagePath));}});holder.checkBox.setSelected(isSelected);}return convertView;}}class ViewHolder {ImageView iv;Button checkBox;}/*** 获取图片路径*/private void getThumbnail() {Cursor mCursor = mContentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[]{MediaStore.Images.ImageColumns.DATA}, "", null,MediaStore.MediaColumns.DATE_ADDED + " DESC");if (mCursor.moveToFirst()) {int _date = mCursor.getColumnIndex(MediaStore.Images.Media.DATA);do {// 获取图片的路径String path = mCursor.getString(_date);Boolean has = false;for (String str : selected) {if (str.equals(path)) {has = true;break;}}if (has) {continue;}imagePathList.add(path);} while (mCursor.moveToNext());}mCursor.close();}@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {if (requestCode == 0) {if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {goCamare();} else {Toast.makeText(this, "请授予相机拍照权限", Toast.LENGTH_SHORT).show();}}}public static void actionStart(Activity activity, ArrayList<String> list, int requestCode) {Intent intent = new Intent(activity, SelectPictureActivity.class);intent.putExtra("selected", list);activity.startActivityForResult(intent, requestCode);}}
以下是跳转到选择图片界面:
Intent intent = new Intent(this, SelectPictureActivity.class);intent.putExtra("selected", selected);startActivityForResult(intent, 0);
以下是当回到当前界面的时候拿出图片路径:
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK && requestCode == 0) {if (data == null) {return;}CommonDialog commonDialog = new CommonDialog(this, "正在提交数据...");commonDialog.show();ArrayList<String> temp = (ArrayList<String>) data.getSerializableExtra(SelectPictureActivity.INTENT_SELECTED_PICTURE);for (String path : temp) {//遍历拿到图片途径,把数据加载到集合里面RecordEntity entity = new RecordEntity();entity.setType(1);entity.setWhoTake("王大锤");entity.setTakeTime(TimeFormartUtils.getCurrentTimeSeconds());entity.setPath(path);entity.setNorthLatitude(String.valueOf(location.getLatitude()));entity.setEastLongtudtude(String.valueOf(location.getLongitude()));recordEntitiesNew.add(entity);recordEntitiesAdapter.add(entity);visorListAdapter.setData(recordEntitiesAdapter);}commonDialog.dismiss();}