头像的更换

布局

<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/btn_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拍照"/>
    
    <Button 
        android:id="@+id/btn_choose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="相册"/>
    
    <ImageView 
        android:id="@+id/image_view"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="center"
        />

    
</LinearLayout>

 

java代码

package com.example.lx0807_camera;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

    private Button btn_phone;
    private Button btn_choose;
    private ImageView image_view;
    private String path = Environment.getExternalStorageDirectory()+"/head.jpg"; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_phone = (Button) findViewById(R.id.btn_phone);
        btn_choose = (Button) findViewById(R.id.btn_choose);
        image_view = (ImageView) findViewById(R.id.image_view);
        
        btn_phone.setOnClickListener(this);
        btn_choose.setOnClickListener(this);
        
        
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_phone:
            //通过隐式跳转  打开相机
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //保存到SD卡的指定目录
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
            //通过接收回传值  得到图片
            startActivityForResult(intent, 99);
            break;
        case R.id.btn_choose:
            //通过隐式跳转  打开相册
            Intent intent2 = new Intent(Intent.ACTION_PICK);
            intent2.setType("image/*");
            startActivityForResult(intent2, 100);
            break;
        default:
            break;
        }
    }
    //接收回传值
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        //拍照
        if (requestCode == 99 && resultCode == RESULT_OK) {
            image_view.setImageURI(Uri.fromFile(new File(path)));
        }
        //相册
        if (requestCode == 100 && resultCode == RESULT_OK) {
            crop(data.getData());
        }
        //裁剪
        if (requestCode == 89 && resultCode == RESULT_OK) {
            image_view.setImageBitmap((Bitmap) data.getParcelableExtra("data"));
        }
    }

    private void crop(Uri data) {
        //裁剪
        Intent intent3 = new Intent("com.android.camera.action.CROP");
        intent3.setDataAndType(data, "image/*");
        intent3.putExtra("crop", "true");
        intent3.putExtra("aspectX", "1");
        intent3.putExtra("aspectY", "1");
        intent3.putExtra("outputX", "249");
        intent3.putExtra("outputY", "249");
        intent3.putExtra("return-data", true);
        startActivityForResult(intent3, 89);
        
    }

    

}

//权限

android.permission.READ_EXTERNAL_STORAGE

android.permission.WRITE_EXTERNAL_STORAGE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值