android 拍照和上传

package com.droidstouch.takephoto;
002 
003import java.io.ByteArrayOutputStream;
004 
005import java.io.File;
006import android.app.Activity;
007import android.content.Intent;
008import android.graphics.Bitmap;
009import android.net.Uri;
010import android.os.Bundle;
011import android.os.Environment;
012import android.provider.MediaStore;
013import android.view.View;
014import android.view.View.OnClickListener;
015import android.widget.Button;
016import android.widget.ImageView;
017 
018public class PhotoActivity extends Activity {
019    public static final int NONE = 0;
020 
021    public static final int PHOTOHRAPH = 1;// 拍照
022 
023    public static final int PHOTOZOOM = 2; // 缩放
024 
025    public static final int PHOTORESOULT = 3;// 结果
026 
027    public static final String IMAGE_UNSPECIFIED = "image/*";
028 
029    ImageView imageView = null;
030 
031    Button button0 = null;
032 
033    Button button1 = null;
034 
035    @Override
036    public void onCreate(Bundle savedInstanceState) {
037 
038        super.onCreate(savedInstanceState);
039 
040        setContentView(R.layout.photo);
041 
042        imageView = (ImageView) findViewById(R.id.imageID);
043 
044        button0 = (Button) findViewById(R.id.btn_01);
045 
046        button1 = (Button) findViewById(R.id.btn_02);
047 
048        button0.setOnClickListener(new OnClickListener() {
049 
050            @Override
051            public void onClick(View v) {
052 
053                Intent intent = new Intent(Intent.ACTION_PICK, null);
054 
055                intent.setDataAndType(
056                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
057                        IMAGE_UNSPECIFIED);
058 
059                startActivityForResult(intent, PHOTOZOOM);
060 
061            }
062 
063        });
064 
065        button1.setOnClickListener(new OnClickListener() {
066 
067            @Override
068            public void onClick(View v) {
069 
070                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
071 
072                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
073                        Environment.getExternalStorageDirectory(), "temp.jpg")));
074 
075                startActivityForResult(intent, PHOTOHRAPH);
076 
077            }
078 
079        });
080 
081    }
082 
083    @Override
084    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
085 
086        if (resultCode == NONE)
087 
088            return;
089 
090        // 拍照
091 
092        if (requestCode == PHOTOHRAPH) {
093 
094            // 设置文件保存路径这里放在跟目录下
095 
096            File picture = new File(Environment.getExternalStorageDirectory()
097                    + "/temp.jpg");
098 
099            startPhotoZoom(Uri.fromFile(picture));
100 
101        }
102 
103        if (data == null)
104 
105            return;
106 
107        // 读取相册缩放图片
108 
109        if (requestCode == PHOTOZOOM) {
110 
111            startPhotoZoom(data.getData());
112 
113        }
114 
115        // 处理结果
116 
117        if (requestCode == PHOTORESOULT) {
118 
119            Bundle extras = data.getExtras();
120 
121            if (extras != null) {
122 
123                Bitmap photo = extras.getParcelable("data");
124 
125                ByteArrayOutputStream stream = new ByteArrayOutputStream();
126 
127                photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 -
128                                                                        // 100)压缩文件
129 
130                imageView.setImageBitmap(photo);
131 
132            }
133 
134        }
135 
136        super.onActivityResult(requestCode, resultCode, data);
137 
138    }
139 
140    public void startPhotoZoom(Uri uri) {
141 
142        Intent intent = new Intent("com.android.camera.action.CROP");
143 
144        intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
145 
146        intent.putExtra("crop", "true");
147 
148        // aspectX aspectY 是宽高的比例
149 
150        intent.putExtra("aspectX", 1);
151 
152        intent.putExtra("aspectY", 1);
153 
154        // outputX outputY 是裁剪图片宽�?
155 
156        intent.putExtra("outputX", 64);
157 
158        intent.putExtra("outputY", 64);
159 
160        intent.putExtra("return-data", true);
161 
162        startActivityForResult(intent, PHOTORESOULT);
163 
164    }
165}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值