compile 'com.foamtrace:photopicker:1.0'
1.清单文件中添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.清单文件中添加activity
<activity
android:name="com.foamtrace.photopicker.PhotoPickerActivity"
android:theme="@style/PhotoPickerTheme"
android:configChanges="orientation|screenSize"/>
<activity android:name="com.foamtrace.photopicker.PhotoPreviewActivity"
android:theme="@style/PhotoPickerTheme"/>
3.style.xml添加
4.colors.xml添加
<color name="photopicker_background">#181819
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.foamtrace.photopicker.ImageCaptureManager;
import com.foamtrace.photopicker.PhotoPickerActivity;
import com.foamtrace.photopicker.PhotoPreviewActivity;
import com.foamtrace.photopicker.SelectModel;
import com.foamtrace.photopicker.intent.PhotoPickerIntent;
import com.foamtrace.photopicker.intent.PhotoPreviewIntent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private int columnWidth;
private ArrayList imagePaths = null;
private GridAdapter gridAdapter;
private GridView gv;
private ImageCaptureManager captureManager; // 相机拍照处理类
private static final int REQUEST_CAMERA_CODE = 11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button danxuan = (Button) findViewById(R.id.danxuan);
Button duoxuan = (Button) findViewById(R.id.duoxuan);
Button paizhao = (Button) findViewById(R.id.paizhao);
gv = (GridView) findViewById(R.id.gv);
//得到GridView中每个ImageView宽高
int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi;
cols = cols < 3 ? 3 : cols;
gv.setNumColumns(cols);
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int columnSpace = getResources().getDimensionPixelOffset(R.dimen.space_size);
columnWidth = (screenWidth - columnSpace * (cols-1)) / cols;
danxuan.setOnClickListener(this);
duoxuan.setOnClickListener(this);
paizhao.setOnClickListener(this);
//GridView item点击事件(浏览照片)
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
PhotoPreviewIntent intent = new PhotoPreviewIntent(MainActivity.this);
intent.setCurrentItem(position);
intent.setPhotoPaths(imagePaths);
startActivityForResult(intent, 22);
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
//单选图片
case R.id.danxuan:
PhotoPickerIntent intent = new PhotoPickerIntent(MainActivity.this);
intent.setSelectModel(SelectModel.SINGLE);
intent.setShowCarema(true);
startActivityForResult(intent, REQUEST_CAMERA_CODE);
break;
//多选图片
case R.id.duoxuan:
PhotoPickerIntent intent1 = new PhotoPickerIntent(MainActivity.this);
intent1.setSelectModel(SelectModel.MULTI);
intent1.setShowCarema(true); // 是否显示拍照
intent1.setMaxTotal(9); // 最多选择照片数量,默认为9
intent1.setSelectedPaths(imagePaths); // 已选中的照片地址, 用于回显选中状态
startActivityForResult(intent1, REQUEST_CAMERA_CODE);
break;
//拍照
case R.id.paizhao:
try {
if(captureManager == null){
captureManager = new ImageCaptureManager(MainActivity.this);
}
Intent intent3 = captureManager.dispatchTakePictureIntent();
startActivityForResult(intent3, ImageCaptureManager.REQUEST_TAKE_PHOTO);
} catch (IOException e) {
Toast.makeText(MainActivity.this, com.foamtrace.photopicker.R.string.msg_no_camera, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
switch (requestCode){
// 选择照片
case REQUEST_CAMERA_CODE:
loadAdpater(data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT));
break;
//浏览照片
case 22:
loadAdpater(data.getStringArrayListExtra(PhotoPreviewActivity.EXTRA_RESULT));
break;
// 调用相机拍照
case ImageCaptureManager.REQUEST_TAKE_PHOTO:
if(captureManager.getCurrentPhotoPath() != null) {
captureManager.galleryAddPic();
ArrayList paths = new ArrayList<>();
paths.add(captureManager.getCurrentPhotoPath());
loadAdpater(paths);
}
break;
}
}
}
private void loadAdpater(ArrayList paths){
if(imagePaths == null){
imagePaths = new ArrayList<>();
}
imagePaths.clear();
imagePaths.addAll(paths);
if(gridAdapter == null){
gridAdapter = new GridAdapter(imagePaths);
gv.setAdapter(gridAdapter);
}else {
gridAdapter.notifyDataSetChanged();
}
}
private class GridAdapter extends BaseAdapter {
private ArrayList listUrls;
public GridAdapter(ArrayList listUrls) {
this.listUrls = listUrls;
}
@Override
public int getCount() {
return listUrls.size();
}
@Override
public String getItem(int position) {
return listUrls.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.item_image, null);
imageView = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(imageView);
// 重置ImageView宽高
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(columnWidth, columnWidth);
imageView.setLayoutParams(params);
}else {
imageView = (ImageView) convertView.getTag();
}
//框架里自带glide
Glide.with(MainActivity.this)
.load(new File(getItem(position)))
.placeholder(R.mipmap.default_error)
.error(R.mipmap.default_error)
.centerCrop()
.crossFade()
.into(imageView);
return convertView;
}
}
}
源码下载:
https://github.com/a2978157/MyPhotoPicker