获取系统图库的图片设置头像

来源:互联网 发布:家具设计用什么软件 编辑:程序博客网 时间:2024/06/12 05:38

所涉及到的内容:

1.从摄像头中获取图像
2.从图库中获取图像
3.对图象进行裁剪
4.对裁剪的图片进行外部存储`

public class MainActivity extends AppCompatActivity {    private static int CAMERA_REQUEST_CODE = 1;    private static int GALLERY_REQUEST_CODE = 2;    private static int CROP_REQUEST_CODE = 3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button button = (Button) findViewById(R.id.button);        //摄像头响应事件        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //打开系统应用的摄像头                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                startActivityForResult(intent,CAMERA_REQUEST_CODE);            }        });        Button viewById = (Button) findViewById(R.id.button2);        //调用系统图库事件        viewById.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);                //该intent 为图像类型                intent.setType("image/*");                startActivityForResult(intent,GALLERY_REQUEST_CODE);            }        });    }    private Uri saveBitmap(Bitmap bm){        File file = new File(Environment.getExternalStorageDirectory()+"/com.jikecuyuan");        //判断该路径是否存在        if (!file.exists()){            //如果不存在创建文件            file.mkdir();        }        //对图片保存的文件对象        File img = new File(file.getAbsolutePath() + "avater.png");        try {            //获取文件的输出流            FileOutputStream fos = new FileOutputStream(img);            //参数 压缩的格式  图片的质量  写入文件的输出流            bm.compress(Bitmap.CompressFormat.PNG,85,fos);            //将输出流刷新,也就是流中全部输出            fos.flush();            //关闭输出流            fos.close();            //将写入的文件转换为File类型的URI            return Uri.fromFile(img);        } catch (FileNotFoundException e) {            e.printStackTrace();            //如果出现异常返回null            return null;        } catch (IOException e) {            e.printStackTrace();            //如果出现异常返回null            return null;        }    }    /**     * 对图象进行裁剪     * @param uri 用有URI对图形进行数据的传递     */    private void startImageZoom(Uri uri){        //启动系统的裁剪界面        Intent intent = new Intent("com.android.camera.action.CROP");        //数据和类型        intent.setDataAndType(uri,"image/*");        //设置为可裁剪的        intent.putExtra("crop","true");        //裁剪的宽高的比例        intent.putExtra("aspectX",1);        intent.putExtra("aspectY",1);        //裁剪的宽高        intent.putExtra("outputX",150);        intent.putExtra("outputY",150);        //裁剪后的数据是通过Intent返回回来的        intent.putExtra("return-data",true);        startActivityForResult(intent,CROP_REQUEST_CODE);    }    private Uri convertUri(Uri uri){        InputStream is = null;        try {            //从URi中获取到输入流            is = getContentResolver().openInputStream(uri);            //将输入流转换为Bitmap            Bitmap bitmap = BitmapFactory.decodeStream(is);            is.close();            //将获取的Uri返回            return saveBitmap(bitmap);        } catch (FileNotFoundException e) {            e.printStackTrace();            return null;        } catch (IOException e) {            e.printStackTrace();            return null;        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        //判断请求码        if (requestCode == CAMERA_REQUEST_CODE){            //判断用户是否拍下照片,若直接按返回则data为null            if (data == null){                return;            }else {                //将获取来的数据转为Bitmap类型                Bundle extras = data.getExtras();                //判断数据是否为空                if (extras != null){                    //将获取的来的资源放置imageView中                    Bitmap data1 = extras.getParcelable("data");                    //获取图像的Url                    Uri uri = saveBitmap(data1);                    //次URI必须是File类型的URi                    startImageZoom(uri);                }            }        }else if (requestCode == GALLERY_REQUEST_CODE){            if (data == null){                return;            }            Uri uri;            uri = data.getData();            //获取 File URI            Uri uri1 = convertUri(uri);            //次URI必须是File类型的URi            startImageZoom(uri1);        }else if (requestCode == CROP_REQUEST_CODE){            if (data == null){                return;            }            //将数据取回            Bundle extras = data.getExtras();            Bitmap data1 = extras.getParcelable("data");            ((ImageView) findViewById(R.id.imageView)).setImageBitmap(data1);        }    } }
1 0
原创粉丝点击