xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····

001package cn.m15.test;
002 
003import java.io.ByteArrayOutputStream;
004import java.io.File;
005import android.app.Activity;
006import android.content.Intent;
007import android.graphics.Bitmap;
008import android.net.Uri;
009import android.os.Bundle;
010import android.os.Environment;
011import android.provider.MediaStore;
012import android.view.View;
013import android.view.View.OnClickListener;
014import android.widget.Button;
015import android.widget.ImageView;
016 
017public class testActivity extends Activity {
018 
019    public static final int NONE = 0;
020    public static final int PHOTOHRAPH = 1;// 拍照
021    public static final int PHOTOZOOM = 2; // 缩放
022    public static final int PHOTORESOULT = 3;// 结果
023 
024    public static final String IMAGE_UNSPECIFIED = "image/*";
025    ImageView imageView = null;
026    Button button0 = null;
027    Button button1 = null;
028 
029    @Override
030    public void onCreate(Bundle savedInstanceState) {
031        super.onCreate(savedInstanceState);
032        setContentView(R.layout.main);
033        imageView = (ImageView) findViewById(R.id.imageID);
034        button0 = (Button) findViewById(R.id.btn_01);
035        button1 = (Button) findViewById(R.id.btn_02);
036 
037        button0.setOnClickListener(new OnClickListener() {
038            @Override
039            public void onClick(View v) {
040                Intent intent = new Intent(Intent.ACTION_PICK, null);
041                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
042                startActivityForResult(intent, PHOTOZOOM);
043            }
044        });
045 
046        button1.setOnClickListener(new OnClickListener() {
047 
048            @Override
049            public void onClick(View v) {
050                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
051                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg")));
052                startActivityForResult(intent, PHOTOHRAPH);
053            }
054        });
055    }
056 
057    @Override
058    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
059        if (resultCode == NONE)
060            return;
061        // 拍照
062        if (requestCode == PHOTOHRAPH) {
063            //设置文件保存路径这里放在跟目录下
064            File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
065            startPhotoZoom(Uri.fromFile(picture));
066        }
067         
068        if (data == null)
069            return;
070         
071        // 读取相册缩放图片
072        if (requestCode == PHOTOZOOM) {
073            startPhotoZoom(data.getData());
074        }
075        // 处理结果
076        if (requestCode == PHOTORESOULT) {
077            Bundle extras = data.getExtras();
078            if (extras != null) {
079                Bitmap photo = extras.getParcelable("data");
080                ByteArrayOutputStream stream = new ByteArrayOutputStream();
081                photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件
082                imageView.setImageBitmap(photo);
083            }
084 
085        }
086 
087        super.onActivityResult(requestCode, resultCode, data);
088    }
089 
090    public void startPhotoZoom(Uri uri) {
091        Intent intent = new Intent("com.android.camera.action.CROP");
092        intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
093        intent.putExtra("crop", "true");
094        // aspectX aspectY 是宽高的比例
095        intent.putExtra("aspectX", 1);
096        intent.putExtra("aspectY", 1);
097        // outputX outputY 是裁剪图片宽高
098        intent.putExtra("outputX", 64);
099        intent.putExtra("outputY", 64);
100        intent.putExtra("return-data", true);
101        startActivityForResult(intent, PHOTORESOULT);
102    }
103}

01<?xml version="1.0" encoding="utf-8"?>
02<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03    android:orientation="vertical" android:layout_width="fill_parent"
04    android:layout_height="fill_parent">
05    <TextView android:layout_width="fill_parent"
06        android:layout_height="wrap_content" android:text="@string/hello" />
07    <ImageView android:id="@+id/imageID"
08        android:adjustViewBounds="true" android:maxWidth="50dip"
09        android:maxHeight="50dip" android:layout_width="wrap_content"
10        android:layout_height="wrap_content" />
11    <Button android:id="@+id/btn_01" android:layout_height="50dip"
12            android:text="相册" android:layout_width="150dip"/>
13    <Button android:id="@+id/btn_02" android:layout_height="50dip"
14            android:text="拍照" android:layout_width="150dip"/>
15</LinearLayout>


posted on 2012-03-15 12:12 小果子 阅读(481) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理