android 拍照和上传
16lz
2021-01-26
package com.droidstouch.takephoto; |
002 |
003 | import java.io.ByteArrayOutputStream; |
004 |
005 | import java.io.File; |
006 | import android.app.Activity; |
007 | import android.content.Intent; |
008 | import android.graphics.Bitmap; |
009 | import android.net.Uri; |
010 | import android.os.Bundle; |
011 | import android.os.Environment; |
012 | import android.provider.MediaStore; |
013 | import android.view.View; |
014 | import android.view.View.OnClickListener; |
015 | import android.widget.Button; |
016 | import android.widget.ImageView; |
017 |
018 | public 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 | } |
更多相关文章
- android 扫描sdcard下的图片
- Android(安卓)自定义Ratingbar
- EditView中插入图片
- HttpHuiApplication--下载图片url,HttpURLConnection,简单版
- clock时钟
- Android相关开发网站
- android 点击图片全屏展示效果
- android 如何利用手势放大和缩小一个图片?
- Android(安卓)7.0拍照权限处理