相机相册

package com.example.lianxi;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private Button bu1,bu2;
    private ImageView imageView;
    private String path=Environment.getExternalStorageDirectory()+"/1603A.png";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bu1=findViewById(R.id.bu);
        bu2=findViewById(R.id.bu2);
        imageView=findViewById(R.id.img);
        bu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
                startActivityForResult(intent,1000);
            }
        });
        bu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent,3000);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1000&&resultCode==RESULT_OK){
            Intent intent=new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(Uri.fromFile(new File(path)),"image/*");
            intent.putExtra("crop",true);
            intent.putExtra("outputX",250);
            intent.putExtra("outputY",250);
            intent.putExtra("aspectX",2);
            intent.putExtra("aspectY",1);
            intent.putExtra("return-data",true);
            startActivityForResult(intent,2000);
        }
        if (requestCode==2000&&resultCode==RESULT_OK){
            Bitmap bitmap=data.getParcelableExtra("data");
            imageView.setImageBitmap(bitmap);
        }
        if (requestCode==3000&&resultCode==RESULT_OK){
            Uri uri = data.getData();
            Intent intent=new Intent("com.android.camera.action.CROP");
            intent.putExtra("crop",true);
            intent.putExtra("aspectX",2);
            intent.putExtra("aspectY",1);
            intent.putExtra("outputX",250);
            intent.putExtra("outputY",250);
            intent.setDataAndType(uri,"image/*");
            intent.putExtra("return-data",true);
            startActivityForResult(intent,4000);
        }
        if (requestCode==4000&&resultCode==RESULT_OK){
            Bitmap bitmap=data.getParcelableExtra("data");
            imageView.setImageBitmap(bitmap);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值