首先我们写好一个布局
用来添加图片的gridview,
```
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private final int RESQUEST=1;
private final int REQUEST_CODE_CAPTURE_CAMEIA=2;
private GridView rv_add_picture;
private Listpics;
private PicAdapter mMPicAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化各种数据
pics=newArrayList<>();
pics.add("file:///android_asset/ic_add_white_gray_small.png");
rv_add_picture= (GridView) findViewById(R.id.rv_add_picture);
mMPicAdapter=newPicAdapter(pics,this);
rv_add_picture.setOnItemClickListener(this);
rv_add_picture.setAdapter(mMPicAdapter);
}
```
//进入相册或相机后回调过来数据
@Override
protected voidonActivityResult(intrequestCode,intresultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if(resultCode == Activity.RESULT_OK) {
if(requestCode ==RESQUEST) {
Uri uriPic = data.getData();
getRealPathFromURI(uriPic);
}else if(requestCode ==REQUEST_CODE_CAPTURE_CAMEIA) {
Uri uri = data.getData();
if(uri ==null) {
//use bundle to get data
Bundle bundle = data.getExtras();
if(bundle !=null) {
Bitmap photo = (Bitmap) bundle.get("data");//get bitmap
String spath = Environment.getExternalStorageDirectory() + File.separator+"gridview"+ File.separator+ System.currentTimeMillis() +".jpeg";
//spath :生成图片取个名字和路径包含类型
saveImage(photo,spath);
}
}
}
}
}
//保存图片到指定路径
public void saveImage(Bitmap photo,String spath) {
try{
BufferedOutputStream bos =newBufferedOutputStream(
newFileOutputStream(spath,false));
pics.add(0,spath);
mMPicAdapter.setData(pics);
bos.flush();
bos.close();
}catch(Exception e) {
e.printStackTrace();
}
}
//获取相册图片路径
public voidgetRealPathFromURI(Uri contentUri) {
String[] filePathColumns = {MediaStore.MediaColumns.DATA};
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(contentUri,filePathColumns,null,null,null);
if(c !=null) {
c.moveToFirst();
intcolumnIndex = c.getColumnIndex(filePathColumns[0]);
String pathImg = c.getString(columnIndex);
c.close();
pics.add(0,pathImg);
mMPicAdapter.setData(pics);
}
}
@Override
public void onItemClick(AdapterView parent,View view,intposition,longid) {
if(position ==pics.size() -1) {
AlertDialog.Builder builder =newAlertDialog.Builder(this)
.setNegativeButton("拍照",newDialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,intwhich) {
String state = Environment.getExternalStorageState();
//如果插入了内存卡的话
if(state.equals(Environment.MEDIA_MOUNTED)) {
Intent getImageByCamera =newIntent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera,REQUEST_CODE_CAPTURE_CAMEIA);
}
else{
Toast.makeText(getApplicationContext(),"请确认已经插入SD卡",Toast.LENGTH_LONG).show();
}
}
}).setPositiveButton("相册",newDialogInterface.OnClickListener() {
@Override
public voidonClick(DialogInterface dialog,intwhich) {
Intent picIntent =newIntent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picIntent,RESQUEST);
}
});
builder.show();
}
}
}
```
这时候我们就可以得到我们想要的功能了,界面不是特别的优雅,可以按照自己的需求,进行修改相应的界面就可以了!
图片1 图二由于用的模拟器,所以图片是这样的!