android截取图片

来源:互联网 发布:楼盘销售软件 编辑:程序博客网 时间:2024/05/18 04:17

应用中经常会有显示用户图像的需求。用户需先截取选择的图片,然后上传服务器(这里略),显示截取的图片。效果类似下图:


phone.xml文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="#fcf3ef"        android:gravity="center_horizontal"        android:orientation="vertical"        android:paddingTop="30dp" >    <ImageView            android:id="@+id/avatar"            android:layout_width="130dp"            android:layout_height="130dp"            android:background="#00ff00" />    <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginTop="30dp"            android:gravity="center_horizontal" >        <LinearLayout                android:id="@+id/local_select_button"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:gravity="center"                android:orientation="vertical" >            <ImageView                    android:layout_width="50dp"                    android:layout_height="30dp"                    android:background="#0000ff" />            <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_marginTop="10dp"                    android:text="本地选择"                    android:textColor="#000000"                    android:textSize="20sp" />        </LinearLayout>        <LinearLayout                android:id="@+id/take_photo_button"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="50dp"                android:gravity="center"                android:orientation="vertical" >            <ImageView                    android:layout_width="50dp"                    android:layout_height="30dp"                    android:background="#ff0000" />            <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_marginTop="10dp"                    android:text="拍照"                    android:textColor="#000"                    android:textSize="20sp" />        </LinearLayout>    </LinearLayout></LinearLayout>
activity代码

public class MainActivity extends Activity {    public final static int PHOTO_ZOOM = 0;    public final static int TAKE_PHOTO = 1;    public final static int PHOTO_RESULT = 2;    public static final String IMAGE_UNSPECIFIED = "image/*";    private String imageDir, tempImg;    private ImageView avatar;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.phone);        tempImg = "inter.png";        avatar = (ImageView) findViewById(R.id.avatar);        // 本地图库选择按钮        LinearLayout upload =                (LinearLayout)findViewById(R.id.local_select_button);        upload.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(Intent.ACTION_PICK);                intent.setType(IMAGE_UNSPECIFIED);                Intent wrapperIntent=Intent.createChooser(intent, null);                startActivityForResult(wrapperIntent, PHOTO_ZOOM);            }        });        // 拍照按钮        LinearLayout takePhoto=                (LinearLayout)findViewById(R.id.take_photo_button);        takePhoto.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                imageDir = "temp.png";                Intent intent=                        new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                intent.putExtra(MediaStore.EXTRA_OUTPUT,                        Uri.fromFile(new File(Environment.getExternalStorageDirectory(), imageDir)));                startActivityForResult(intent, TAKE_PHOTO);            }        });    }    // 图片缩放截取    public void photoZoom(Uri uri) {        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(uri, IMAGE_UNSPECIFIED);        intent.putExtra("crop", "true");        // aspectX aspectY 是宽高的比例        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        // outputX outputY 是裁剪图片宽高        intent.putExtra("outputX", 250);        intent.putExtra("outputY", 250);        //intent.putExtra("return-data", true);        intent.putExtra("return-data", false);        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), tempImg)));        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());        startActivityForResult(intent, PHOTO_RESULT);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (resultCode == RESULT_OK) {            if (requestCode == PHOTO_ZOOM) {                photoZoom(data.getData());            }            if (requestCode == TAKE_PHOTO) {                File picture = new File(Environment.getExternalStorageDirectory() + "/" + imageDir);                photoZoom(Uri.fromFile(picture));            }            if (requestCode == PHOTO_RESULT) {//                Bundle extras = data.getExtras();//                if (extras != null) {//android.os.TransactionTooLargeException//                    Bitmap photo = extras.getParcelable("data");//                    ByteArrayOutputStream stream = new ByteArrayOutputStream();//                    photo.compress(Bitmap.CompressFormat.PNG, 75, stream);//                    avatar.setImageBitmap(photo);//                }                Bitmap photo = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + tempImg);                avatar.setImageBitmap(photo);            }        }        super.onActivityResult(requestCode, resultCode, data);    }

注意,通过Intent不能传输过大的文件(1M),否则会报android.os.TransactionTooLargeException这个异常。所以此处先将截取的图片存储到sdcard上,然后再显示,这样就不用担心图片大小问题,当然,这里应当判断下sdcard是否存在


0 0
原创粉丝点击