Android7.0调用系统拍照、系统相册(图片压缩、查看原图、权限处理)

本文介绍了如何在Android7.0中调用系统相机和相册,包括权限处理、图片压缩和查看原图功能。通过PopupWindow实现交互,详细讲解了Android6.0权限申请过程。同时,使用Luban库进行图片压缩,并利用PhotoView展示查看原图的功能。提供完整源码下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果以及需求

这里写图片描述这里写图片描述

查看原图可点击放大,可两指放大等
这里写图片描述这里写图片描述

1.PopupWindow的简单使用

PopupWindow最基本的三个条件是一定要设置的:View contentView,int width, int height
showAtLocation 窗口展示的位置
setOutsideTouchable(true)点击外面关闭PopupWindow,一定要在showAtLocation之前调用

View contentView = LayoutInflater.from(this).inflate(R.layout.pop_pic_photo, null);
popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(layout, Gravity.BOTTOM, 0, 0);

TextView load = contentView.findViewById(R.id.load);
load.setOnClickListener(this);

2.Android6.0权限申请

// 现在清单文件进行声明
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
// 权限申请
ActivityCompat.requestPermissions(,new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode );

// 处理权限回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    // 相机权限授予
    grantResults[0] == PackageManager.PERMISSION_GRANTED;
    // 存储权限授予
    grantResults[1] == PackageManager.PERMISSION_GRANTED;
}

3.调用系统相册

// 开启系统相册
Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 123);

// onActivityResult进行回调得到图片路径

if (requestCode == 123 && resultCode == Activity.RESULT_OK) {

    Uri selectedImage = data.getData();
    String[] filePathColumns = {MediaStore.Images.Media.DATA};
    Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
    c.moveToFirst();
    int columnIndex = c.getColumnIndex(filePathColumns[0]);
    // 图片路径
    String imagePath = c.getString(columnIndex);
    c.close();
}

4.调用系统相机

protected void open_camra() {

        Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        String imageFileName = System.currentTimeMillis() + ".png";

        String path = Environment.getExternalStorageDirectory();

        // 定义生成图片的路径(全局)
        StringpicPath = path+imageFileName;

        Uri mImageCaptureUri;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //android7.0以上
            ContentValues contentValues = new ContentValues(1);
            contentValues.put(MediaStore.Images.Media.DATA, new File(path, imageFileName).getAbsolutePath());
            mImageCaptureUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        } else {
            mImageCaptureUri = Uri.fromFile(new File(path, imageFileName));
        }

        takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
        startActivityForResult(takeIntent, 456);
}


// onActivityResult进行回调得到图片路径

if (requestCode == 456 && resultCode == Activity.RESULT_OK) {

    // 图片路径
    StringpicPath
}

5.图片压缩 Luban(鲁班)

Luban.with(this)
        .load(photos)                                   // 传人要压缩的图片列表
        .ignoreBy(100)                                  // 忽略不压缩图片的大小
        .setTargetDir(getPath())                        // 设置压缩后文件存储位置
        .setCompressListener(new OnCompressListener() { //设置回调
          @Override
          public void onStart() {
            // TODO 压缩开始前调用,可以在方法内启动 loading UI
          }

          @Override
          public void onSuccess(File file) {
            // TODO 压缩成功后调用,返回压缩后的图片文件
          }

          @Override
          public void onError(Throwable e) {
            // TODO 当压缩过程出现问题时调用
          }
        }).launch();    //启动压缩

6.图片展示

protected void showImage(File file, ImageView img) {

        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        img.setImageBitmap(bitmap);
        img.setScaleType(ImageView.ScaleType.CENTER_CROP);
}

7.查看原图(压缩后的) PhotoView

// root build.gradle
allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

// module build.gradle
compile 'com.github.chrisbanes:PhotoView:2.1.3'

<com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/photo_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


photoView.setImageBitmap(BitmapFactory.decodeFile(path));

8.下载完整源码 点此下载源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值