调用系统的相机和相册以及压缩图片

本文介绍了如何在Android应用中实现拍照与录像功能,包括通过Intent启动相机进行拍照和录像,以及如何处理和压缩获取到的照片和视频数据。

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

Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE):访问相机照相
Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE):跳转到录像的界面
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("需要存储照片(录像)的文件的名,以及位置")));
Bitmap bitmap=data.getParcelableExtra("data"):直接获取照相数据并转化成bitmap
//图片质量压缩
private Bitmap compressPic1(Bitmap bitmap){
    ByteArrayOutputStream ouput=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,ouput);//压缩质量,言外之意就是越小压缩的越小
    int options=100;
    while(ouput.toByteArray().length/1024>100){
        ouput.reset();
        options-=1;
        if(options==1){
            break;
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG,options,ouput);
    }
    bitmap.recycle();

   return 压缩后的图片数据;

}
//图片比例压缩
private Bitmap compressPic(){
    BitmapFactory.Options options=new BitmapFactory.Options();
    options.inSampleSize=8;   //2的n次幂压缩,图片比列压缩,是原图片的分辨率的1/8
    return  BitmapFactory.decodeFile(new File("照片的本地地址").toString(),options);
}

转载于:https://my.oschina.net/u/2987490/blog/776485

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值