Android 自定义拍照+剪切+相册选择一张或多张图片

一、集成

在build中集成框架位置如下图:

Android 自定义拍照+剪切+相册选择一张或多张图片_第1张图片

 //图片框架
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
    //导入相册多图片选择库
    implementation 'com.github.donkingliang:ImageSelector:2.1.1'

二、实现效果

Android 自定义拍照+剪切+相册选择一张或多张图片_第2张图片

 主页面MainActivity的布局XML文件代码activity_main.xml:




    

MainActivity代码:

package com.zdmtech.photoalbumimgdemo;

import android.view.View;
import android.widget.Button;

import com.zdmtech.photoalbumimgdemo.activity.AlbumActivity;
import com.zdmtech.photoalbumimgdemo.activity.PhotoActivity;
import com.zdmtech.photoalbumimgdemo.activity.PhotoCropActivity;
import com.zdmtech.photoalbumimgdemo.activity.autoCamera.AutoCameraActivity;
import com.zdmtech.photoalbumimgdemo.base.BaseActivity;
import com.zdmtech.photoalbumimgdemo.camera2.PhotoImageSelectActivity;

/**
 * 自定义拍照+剪切+相册选择一张图片
 * */
public class MainActivity extends BaseActivity implements View.OnClickListener {
    private Button btn_photo, btn_album, btn_photo_crop;
    private Button btn_albumMore ;

    @Override
    public int intiLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        btn_photo = findViewById(R.id.btn_photo);
        btn_photo_crop = findViewById(R.id.btn_photo_crop);
        btn_album = findViewById(R.id.btn_album);
        btn_albumMore = findViewById(R.id.btn_albumMore);
    }

    @Override
    public void initData() {
        btn_photo.setOnClickListener(this);
        btn_photo_crop.setOnClickListener(this);
        btn_album.setOnClickListener(this);
        btn_albumMore.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_photo: //拍照
                goToNextUI(PhotoActivity.class, false, null);
                break;
            case R.id.btn_photo_crop: //拍照+裁剪
                goToNextUI(PhotoCropActivity.class, false, null);
                break;
            case R.id.btn_album: //相册
                goToNextUI(AlbumActivity.class, false, null);
                break;
            case R.id.btn_albumMore: //相册自定义(多张)
                goToNextUI(PhotoImageSelectActivity.class, false, null);
                break;
            default:
                break;
        }
    }

}

其中基类BaseActivity代码:

package com.zdmtech.photoalbumimgdemo.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.zdmtech.photoalbumimgdemo.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseActivity extends AppCompatActivity {
    public Context context;
    /**
     * 获取TAG的activity名称
     */
    protected final String TAG = this.getClass().getSimpleName();
    /**
     * 动态获权请求值
     * */
    private int REQUEST_CODE_PERMISSION = 0x00099;

    /**
     * 屏幕横竖屏显示
     * true : 默认竖屏
     * false: 横屏
     */
    private boolean isAllowScreenRoate = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        //设置布局
        setContentView(intiLayout());
        //设置屏幕是否可旋转
        if (!isAllowScreenRoate) {//强制横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else { //强制竖屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        //初始化控件
        initView();
        //设置数据
        initData();
    }
    /**
     * 设置布局
     *
     * @return
     */
    public abstract int intiLayout();

    /**
     * 初始化布局
     */
    public abstract void initView();

    /**
     * 设置数据
     */
    public abstract void initData();

    /**
     * 跳转到下一个界面
     */
    public void goToNextUI(Class cls, boolean close, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        if (close) {
            finish();
        }
    }

    /**
     * 跳转到下一个界面、带返回值
     */
    public void goToNextUIResult(Class cls, int request, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, request);
    }

    /**
     * 状态栏
     */
    public void initSystemBar(Boolean isLight) {
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            if (isLight) {
                window.setStatusBarColor(getResources().getColor(R.color.white));
            } else {
                window.setStatusBarColor(getResources().getColor(R.color.bgColor));
            }

            //状态栏颜色接近于白色,文字图标变成黑色
            View decor = window.getDecorView();
            int ui = decor.getSystemUiVisibility();
            if (isLight) {
                //light --> a|=b的意思就是把a和b按位或然后赋值给a,   按位或的意思就是先把a和b都换成2进制,然后用或操作,相当于a=a|b
                ui |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            } else {
                //dark  --> &是位运算里面,与运算,  a&=b相当于 a = a&b,  ~非运算符
                ui &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
            decor.setSystemUiVisibility(ui);
        }
    }
    /**
     * 显示Toast提示
     */
    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    /**
     * 请求权限
     * 动态获权
     * @param permissions 请求的权限
     * @param requestCode 请求权限的请求码
     */
    public void requestPermission(String[] permissions, int requestCode) {
        this.REQUEST_CODE_PERMISSION = requestCode;
        if (checkPermissions(permissions)) {
            permissionSuccess(REQUEST_CODE_PERMISSION);
        } else {
            try {
                List needPermissions = getDeniedPermissions(permissions);
                ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE_PERMISSION);
            } catch (Exception e) {
                Log.e("BaseActivity", "获取权限失败 Exception = " + e);
            }

        }
    }

    /**
     * 检测所有的权限是否都已授权
     */
    private boolean checkPermissions(String[] permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取权限集中需要申请权限的列表
     */
    private List getDeniedPermissions(String[] permissions) {
        List needRequestPermissionList = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) !=
                    PackageManager.PERMISSION_GRANTED ||
                    ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                needRequestPermissionList.add(permission);
            }
        }
        return needRequestPermissionList;
    }

    /**
     * 系统请求权限回调
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_PERMISSION) {
            if (verifyPermissions(grantResults)) {
                permissionSuccess(REQUEST_CODE_PERMISSION);
            } else {
                permissionFail(REQUEST_CODE_PERMISSION);
            }
        }
    }

    /**
     * 确认所有的权限是否都已授权
     */
    private boolean verifyPermissions(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 获取权限成功
     */
    public void permissionSuccess(int requestCode) {
        Log.e(TAG, "获取权限成功=" + requestCode);

    }

    /**
     * 权限获取失败
     */
    public void permissionFail(int requestCode) {
        Log.e(TAG, "获取权限失败=" + requestCode);
    }

    /**
     * 显示提示对话框
     */
    protected void showTipsDialog() {
        new AlertDialog.Builder(this)
                .setTitle("提示信息")
                .setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        startAppSettings();
                    }
                }).show();
    }

    /**
     * 启动当前应用设置页面
     */
    private void startAppSettings() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }
    /**
     * 通过设置全屏,设置状态栏透明 *
     * * @param activity
     */
    private void fullScreen(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色
                Window window = activity.getWindow();
                View decorView = window.getDecorView();
                // 两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                decorView.setSystemUiVisibility(option);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
                //导航栏颜色也可以正常设置
                window.setNavigationBarColor(Color.TRANSPARENT);
            } else {
                Window window = activity.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
                attributes.flags |= flagTranslucentStatus;
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
    }

    //删除文件及文件夹
    public void deleteDirWihtFile(File dir) {
        if (dir == null || !dir.exists() || !dir.isDirectory())
            return;
        for (File file : dir.listFiles()) {
            if (file.isFile())
                file.delete(); // 删除所有文件
            else if (file.isDirectory())
                deleteDirWihtFile(file); // 递规的方式删除文件夹
        }
        dir.delete();// 删除目录本身
    }

}

        其中实现公共的一些方法,比如:跳转到下一个页面、跳转下一个页面并带回返回值、设置状态栏、Toast提示、动态请求权限方法、删除文件及文件夹等等。

        当然,这里还有许多的类以及工具类,比如拍照页面、相册选择页面等等,具体项目接口如下图:

Android 自定义拍照+剪切+相册选择一张或多张图片_第3张图片

         由于代码内容比较多,小伙伴们可以点击此项目Demo下载代码,可以直接解压在Android Studio中直接运行代码,安装apk使用这些功能了。

你可能感兴趣的:(android,android,studio,glide)