1.打开相机以及查看相册都需要对应的权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//申请WRITE_EXTERNAL_STORAGE权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
} else {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//申请成功,可以拍照
camera();
}
return;
}
}
public void camera(){
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/IMG_" + System.currentTimeMillis() + ".jpg");
if (file.exists()) {
file.delete();
}
//指定uri存储相片
getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
uri = Uri.fromFile(file);
startActivityForResult(getImageByCamera, RESULT_CAMERA);
}
拍照成功的回调:
if (requestCode == RESULT_CAMERA) {
// //拍照成功的回调
Intent intent = new Intent(MainActivity.this,WritingActivity.class);
intent.putExtra("data2", uri.toString());
startActivity(intent);
}
2.android自带的相册是不能多选的,想要多选就必须自定义相册,创造自定义相册有比较重要的几个方法:
/*
获取相册列表
*/
public List getAlbums() {
List albums = new ArrayList();
Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, MediaStore.Images.ImageColumns.SIZE}, null, null, null);
if (cursor == null || !cursor.moveToNext())
return new ArrayList();
cursor.moveToLast();
PhotoDerictoryBean photoDerictoryBean = null;
boolean falg = true;
do {
falg = true;
if (cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.SIZE)) < 1024 * 10)
continue;
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME));
photoDerictoryBean = new PhotoDerictoryBean(name);
photoDerictoryBean.setImg(cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)));
photoDerictoryBean.setNum(1);
for (PhotoDerictoryBean p : albums) {
if (p.getName().equals(photoDerictoryBean.getName())) {
p.setNum(p.getNum() + 1);
falg = false;
}
}
if (falg) {
albums.add(photoDerictoryBean);
}
} while (cursor.moveToPrevious());
if (cursor != null) {
cursor.close();
}
return albums;
}
/*
根据名字获取对应相册下的图片
*/
public List getAlbum(String name) {
Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_ADDED, MediaStore.Images.ImageColumns.SIZE}, "bucket_display_name = ?",
new String[]{name}, MediaStore.Images.ImageColumns.DATE_ADDED);
if (cursor == null || !cursor.moveToNext())
return new ArrayList();
List photos = new ArrayList();
cursor.moveToLast();
do {
if (cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.SIZE)) > 1024 * 10) {
photos.add(new PhotoBean(cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)), true, false));
}
} while (cursor.moveToPrevious());
if (cursor != null) {
cursor.close();
}
return photos;
}
//获取最近照片
public List getCurrent() {
Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.DATE_ADDED, MediaStore.Images.ImageColumns.SIZE}, null, null, MediaStore.Images.ImageColumns.DATE_ADDED);
if (cursor == null || !cursor.moveToNext())
return new ArrayList();
List photos = new ArrayList();
cursor.moveToLast();
PhotoBean bean = null;
do {
if (cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.SIZE)) > 1024 * 10) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
bean = new PhotoBean(path, true, false);
photos.add(bean);
}
} while (cursor.moveToPrevious());
if (cursor != null) {
cursor.close();
}
return photos;
}
3.拿到图片资源了,剩下的页面逻辑自己处理好就没问题了,不会处理?来来来,靠近点,我慢慢给你说...
Demo地址:http://download.csdn.net/download/qq_34963282/9957511