Android 多媒体应用:开启摄像头、查看相册中的照片

开启摄像头

开启摄像头是在ACtivity中打开系统自带的摄像头应用。

主函数代码
private Button mButtonStartCamera;
    private ImageView mImageView;
    private File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButtonStartCamera = (Button) findViewById(R.id.button_start_camera);
        mImageView = (ImageView) findViewById(R.id.imageview);
        mButtonStartCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机
                file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
                try {
                    file.createNewFile();

                } catch (IOException e) {
                    e.printStackTrace();
                }
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//告诉系统相机将照片保存的位置
                startActivityForResult(intent, 0x23);//开始启动
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==0x23) {
            if (resultCode==RESULT_OK){
            //有些手机不能显示出拍出来的图片是因为图片过大,所以在显示之前需要把图片压缩一下——————ImageZip.zipImage(file.getAbsolutePath());//压缩图片
                mImageView.setImageURI(Uri.fromFile(file));//得到图片
            }

        }
    }
布局
<Button
        android:id="@+id/button_start_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="调用系统相机"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageview"/>

注意:在manifests中加权限

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

压缩图片的方法

public class ImageZip {
    public static void zipImage(String savePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(savePath, options);
        options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
        try {
            FileOutputStream fos = new FileOutputStream(savePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        bitmap.recycle();
        bitmap = null;
        System.gc();
    }
    public static int computeSampleSize(BitmapFactory.Options options,
                                 int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,
                maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options,
                                         int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                .sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

}

压缩图片中后面两个方法是Google的压缩图片,第一个是老师写的

查看相册中照片的代码
mButtonOpenGallery= (Button) findViewById(R.id.button_open_gallery);
        mButtonOpenGallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Intent.ACTION_GET_CONTENT);//启动存有相册的活动
                intent.setType("image/*");
                startActivityForResult(intent,GET_PIC_FORM_GALLERY);
            }
        });
  //      *********************************
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK) {
            switch(requestCode){
                case 0x23:
                ImageZip.zipImage(file.getAbsolutePath());//压缩图片
                mImageView.setImageURI(Uri.fromFile(file));//得到图片
                break;
                case GET_PIC_FORM_GALLERY:
                    Uri uri=data.getData();
                    mImageView.setImageURI(uri);
                    break;
            }

        }
    }
压缩相册里的原有图片的方法
public String getFilePathFromUri(Uri uri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = managedQuery(uri, proj, null, null, null);
        int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(index);
        cursor.close();
        cursor = null;
        return path;
    }

然后只需要在主函数里调用这个方法就行了

case GET_PIC_FORM_GALLERY:

                    Uri uri = data.getData();
                    //根据Uri在数据库中查询出文件详细信息,从数据库中查出详细路径,然后在压缩
                    String s = getFilePathFromUri(uri);
                    ImageZip.zipImage(s);//压缩图片-----ImageZip这个类已经在上面写了。
                    mImageView.setImageURI(uri);
                    break;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值