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

本文介绍了如何在Android应用中使用系统相机拍摄照片,并将照片保存到设备的存储中。通过设置Intent来启动相机应用,获取拍摄的照片路径,并在主界面上显示拍摄的照片。

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

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" tools:context=".MainActivity">

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

</LinearLayout>
public class MainActivity extends AppCompatActivity {
    private Button mButtonStart;
    private ImageView mImageView;
    private  File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView= (ImageView) findViewById(R.id.imageview);
        mButtonStart= (Button) findViewById(R.id.button_start_camera);
        mButtonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机
                file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
                if (file==null){
                    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){
                mImageView.setImageURI(Uri.fromFile(file));//得到图片
            }
        }
    }
}
public class MainActivity extends AppCompatActivity {
    private Button mButtonStart;
    private Button mButtonGarrery;
    private ImageView mImageView;
    private  File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView= (ImageView) findViewById(R.id.imageview);
        mButtonStart= (Button) findViewById(R.id.button_start_camera);
        mButtonGarrery= (Button) findViewById(R.id.button_start_gallery);
        mButtonGarrery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //调用系统图库
                Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent,0x24);
            }
        });
        mButtonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机
                file=new File(Environment.getExternalStorageDirectory()+"/Pictures",System.currentTimeMillis()+".jpg");
                if (!file.exists()){
                    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){
                zipImage(file.getAbsolutePath());
                mImageView.setImageURI(Uri.fromFile(file));//得到图片
            }
        }else if (requestCode==0x24){
            if (resultCode==RESULT_OK){
                Uri uri=data.getData();
                mImageView.setImageURI(uri);
            }
        }
    }
    //---------------压缩图片--------------------
    private 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 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 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;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值