Skip to content

Commit

Permalink
1、增加拍照功能。2、使用Builder的方式调用相册。
Browse files Browse the repository at this point in the history
  • Loading branch information
梁任彦 committed Jun 25, 2018
1 parent 58d798f commit 15f5a43
Show file tree
Hide file tree
Showing 19 changed files with 511 additions and 135 deletions.
14 changes: 14 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.donkingliang.imageselectdemo">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

<application
android:allowBackup="true"
Expand All @@ -29,6 +31,18 @@
android:name="com.donkingliang.imageselector.ClipImageActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

<!-- Android 7.0 文件共享配置,必须配置 -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.donkingliang.imageselectdemo.adapter.ImageAdapter;
import com.donkingliang.imageselector.utils.ImageSelector;
import com.donkingliang.imageselector.utils.ImageSelectorUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -39,7 +41,7 @@ protected void onCreate(Bundle savedInstanceState) {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && data != null) {
ArrayList<String> images = data.getStringArrayListExtra(ImageSelectorUtils.SELECT_RESULT);
ArrayList<String> images = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
mAdapter.refresh(images);
}
}
Expand All @@ -49,27 +51,48 @@ public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_single:
//单选
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, true, 0);
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, true, 0);
ImageSelector.builder()
.useCamera(true) // 设置是否使用拍照
.setSingle(true) //设置是否单选
.start(this, REQUEST_CODE); // 打开相册
break;

case R.id.btn_limit:
//多选(最多9张)
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 9);
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 9);
// ImageSelector.builder().setSingle(true).start(this,REQUEST_CODE);
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 9, mAdapter.getImages()); // 把已选的传入。
ImageSelector.builder()
.useCamera(true) // 设置是否使用拍照
.setSingle(false) //设置是否单选
.setMaxSelectCount(9) // 图片的最大选择数量,小于等于0时,不限数量。
.start(this, REQUEST_CODE); // 打开相册
break;

case R.id.btn_unlimited:
//多选(不限数量)
ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE);
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE);
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, mAdapter.getImages()); // 把已选的传入。
//或者
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 0);
// ImageSelectorUtils.openPhoto(MainActivity.this, REQUEST_CODE, false, 0, mAdapter.getImages()); // 把已选的传入。

ImageSelector.builder()
.useCamera(true) // 设置是否使用拍照
.setSingle(false) //设置是否单选
.setMaxSelectCount(0) // 图片的最大选择数量,小于等于0时,不限数量。
.start(this, REQUEST_CODE); // 打开相册
break;

case R.id.btn_clip:
//单选并剪裁
ImageSelectorUtils.openPhotoAndClip(MainActivity.this, REQUEST_CODE);
// ImageSelectorUtils.openPhotoAndClip(MainActivity.this, REQUEST_CODE);
ImageSelector.builder()
.useCamera(true) // 设置是否使用拍照
.setCrop(true) // 设置是否使用图片剪切功能。
.setSingle(true) //设置是否单选
.start(this, REQUEST_CODE); // 打开相册
break;
}
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>

<!-- 这个是保存拍照图片的路径,必须配置。 -->
<external-path
name="images"
path="Pictures" />
</paths>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.donkingliang.imageselector.utils.ImageSelectorUtils;
import com.donkingliang.imageselector.utils.ImageSelector;
import com.donkingliang.imageselector.utils.ImageUtil;
import com.donkingliang.imageselector.utils.StringUtils;
import com.donkingliang.imageselector.view.ClipImageView;
Expand All @@ -33,10 +33,13 @@ protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_clip_image);

mRequestCode = getIntent().getIntExtra("requestCode", 0);
Intent intent = getIntent();
mRequestCode = intent.getIntExtra("requestCode", 0);

setStatusBarColor();
ImageSelectorUtils.openPhoto(this, mRequestCode, true, 0);
ImageSelectorActivity.openActivity(this, mRequestCode, true,
intent.getBooleanExtra(ImageSelector.USE_CAMERA, true), 0,
intent.getStringArrayListExtra(ImageSelector.SELECTED));
initView();
}

Expand Down Expand Up @@ -78,7 +81,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (data != null && requestCode == mRequestCode) {
ArrayList<String> images = data.getStringArrayListExtra(ImageSelectorUtils.SELECT_RESULT);
ArrayList<String> images = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
Bitmap bitmap = ImageUtil.decodeSampledBitmapFromFile(images.get(0), 720, 1080);
if (bitmap != null) {
imageView.setBitmapData(bitmap);
Expand All @@ -102,15 +105,17 @@ private void confirm(Bitmap bitmap) {
ArrayList<String> selectImages = new ArrayList<>();
selectImages.add(imagePath);
Intent intent = new Intent();
intent.putStringArrayListExtra(ImageSelectorUtils.SELECT_RESULT, selectImages);
intent.putStringArrayListExtra(ImageSelector.SELECT_RESULT, selectImages);
setResult(RESULT_OK, intent);
}
finish();
}

public static void openActivity(Activity context, int requestCode) {
public static void openActivity(Activity context, int requestCode, boolean useCamera, ArrayList<String> selected) {
Intent intent = new Intent(context, ClipImageActivity.class);
intent.putExtra("requestCode", requestCode);
intent.putExtra(ImageSelector.USE_CAMERA, useCamera);
intent.putExtra(ImageSelector.SELECTED, selected);
context.startActivityForResult(intent, requestCode);
}
}
Loading

0 comments on commit 15f5a43

Please sign in to comment.