文章目录星光不负赶路人,时间不负有心人
- Android---图片相册选择和相机拍照
- 效果
- 获取权限,导入依赖
- 危险权限和普通权限
- 获取权限的案例
- 读取用户相册
- 点击事件读取相册
// 图片选择控件 implementation 'io.github.lucksiege:pictureselector:v2.7.2' // 让用户同意权限的d窗如何调出来呢,我们使用第三方开源控件 implementation 'com.yanzhenjie:permission:2.0.3' // 网络图片的加载 implementation 'com.github.bumptech.glide:glide:4.12.0'
- 增加文件读写的权限
- 第三个是照相机的权限
android:requestLegacyExternalStorage="true"
危险权限和普通权限文件的读写权限和我们以前学习的网络权限等等有些区别,它属于危险权限
从 Android6.0 开始,Google调整了应用的权限申请方案。调整之后将权限分级,分成了普通权限和危险权限普通权限的授权方式跟之前一样,只需要在 Manifest 文件中申明即可
危险权限不仅需要在 Manifest 文件中声明,还需要在程序中调用官方提供的Api主动申请
危险权限有如下九种:读取联系人;打电话;读取用户日历信息;相机权限;传感器,获取用户位置;读取用户SD卡(拍的照片视频都在这里);多媒体(录音);短信。
AndPermission.with(this) .runtime() .permission(Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA) .onGranted(permissions -> { Toast.makeText(MainActivity2.this, "授权成功!", Toast.LENGTH_SHORT).show(); }) .onDenied(permissions -> { //用户拒绝->强制退出app Toast.makeText(MainActivity2.this, "未获取权限不能使用本app!", Toast.LENGTH_SHORT).show(); System.exit(0); }) .start();
如果用户不允许以上权限,用户拒绝,市面上大多数APP则会直接退出。
读取用户相册建GlideEngine.java类
官方代码,直接使用
package com.huncm.xiaotang; import android.content.Context; import android.graphics.Bitmap; import android.graphics.PointF; import android.graphics.drawable.Drawable; import android.view.View; import android.widget.ImageView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.graphics.drawable.RoundedBitmapDrawable; import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; import com.bumptech.glide.Glide; import com.bumptech.glide.request.target.BitmapImageViewTarget; import com.bumptech.glide.request.target.ImageViewTarget; import com.luck.picture.lib.engine.ImageEngine; import com.luck.picture.lib.listener.OnImageCompleteCallback; import com.luck.picture.lib.tools.MediaUtils; import com.luck.picture.lib.widget.longimage.ImageSource; import com.luck.picture.lib.widget.longimage.ImageViewState; import com.luck.picture.lib.widget.longimage.SubsamplingScaleImageView; public class GlideEngine implements ImageEngine { @Override public void loadImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) { Glide.with(context) .load(url) .into(imageView); } @Override public void loadImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView, SubsamplingScaleImageView longImageView, OnImageCompleteCallback callback) { Glide.with(context) .asBitmap() .load(url) .into(new ImageViewTarget点击事件读取相册(imageView) { @Override public void onLoadStarted(@Nullable Drawable placeholder) { super.onLoadStarted(placeholder); if (callback != null) { callback.onShowLoading(); } } @Override public void onLoadFailed(@Nullable Drawable errorDrawable) { super.onLoadFailed(errorDrawable); if (callback != null) { callback.onHideLoading(); } } @Override protected void setResource(@Nullable Bitmap resource) { if (callback != null) { callback.onHideLoading(); } if (resource != null) { boolean eqLongImage = MediaUtils.isLongImg(resource.getWidth(), resource.getHeight()); longImageView.setVisibility(eqLongImage ? View.VISIBLE : View.GONE); imageView.setVisibility(eqLongImage ? View.GONE : View.VISIBLE); if (eqLongImage) { // 加载长图 longImageView.setQuickScaleEnabled(true); longImageView.setZoomEnabled(true); longImageView.setDoubleTapZoomDuration(100); longImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP); longImageView.setDoubleTapZoomDpi(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER); longImageView.setImage(ImageSource.bitmap(resource), new ImageViewState(0, new PointF(0, 0), 0)); } else { // 普通图片 imageView.setImageBitmap(resource); } } } }); } @Override public void loadImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView, SubsamplingScaleImageView longImageView) { Glide.with(context) .asBitmap() .load(url) .into(new ImageViewTarget (imageView) { @Override protected void setResource(@Nullable Bitmap resource) { if (resource != null) { boolean eqLongImage = MediaUtils.isLongImg(resource.getWidth(), resource.getHeight()); longImageView.setVisibility(eqLongImage ? View.VISIBLE : View.GONE); imageView.setVisibility(eqLongImage ? View.GONE : View.VISIBLE); if (eqLongImage) { // 加载长图 longImageView.setQuickScaleEnabled(true); longImageView.setZoomEnabled(true); longImageView.setDoubleTapZoomDuration(100); longImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP); longImageView.setDoubleTapZoomDpi(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER); longImageView.setImage(ImageSource.bitmap(resource), new ImageViewState(0, new PointF(0, 0), 0)); } else { // 普通图片 imageView.setImageBitmap(resource); } } } }); } @Override public void loadFolderImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) { Glide.with(context) .asBitmap() .load(url) .override(180, 180) .centerCrop() .sizeMultiplier(0.5f) .placeholder(R.drawable.picture_image_placeholder) .into(new BitmapImageViewTarget(imageView) { @Override protected void setResource(Bitmap resource) { RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory. create(context.getResources(), resource); circularBitmapDrawable.setCornerRadius(8); imageView.setImageDrawable(circularBitmapDrawable); } }); } @Override public void loadAsGifImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) { Glide.with(context) .asGif() .load(url) .into(imageView); } @Override public void loadGridImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) { Glide.with(context) .load(url) .override(200, 200) .centerCrop() .placeholder(R.drawable.picture_image_placeholder) .into(imageView); } private GlideEngine() { } private static GlideEngine instance; public static GlideEngine createGlideEngine() { if (null == instance) { synchronized (GlideEngine.class) { if (null == instance) { instance = new GlideEngine(); } } } return instance; } }
ImageView imageView = findViewById(R.id.imageViewhjc); findViewById(R.id.buttonhjc).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PictureSelector.create(MainActivity.this) .openGallery(PictureMimeType.ofAll()) .imageEngine(GlideEngine.createGlideEngine()) .forResult(new OnResultCallbackListener() { @Override public void onResult(List result) { // onResult Callback for (LocalMedia localMedia : result){ Log.i("MainActivity",localMedia.getPath()); } System.out.println(result.toString()); Glide.with(MainActivity.this).load(Uri.fromFile(new File(result.get(0).getRealPath()))).into(imageView); } @Override public void onCancel() { // onCancel Callback } }); } });
我们点击相册中的图片通过回调函数可以拿到图片的本地路径 /storage/emulated/0/DCIM
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)