1 import java.io.File;
2 import android.app.Activity;
3 import android.content.Intent;
4 import android.graphics.drawable.Drawable;
5 import android.net.Uri;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.Button;
10
11 public class GalleryActivity extends Activity {
12
13 private static int SELECT_PICTURE;//返回标志位 filed
14
15 private File tempFile;
16
17 Button button;
18
19 /** Called when the activity is first created. */
20 @Override
21 public void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 this.tempFile = new File("/sdcard/a.jpg");// 这句一定要在onCreate()里面调用
24 button = new Button(this);
25 button.setText("获取图片");
26 button.setOnClickListener(new OnClickListener() {
27 @Override
28 public void onClick(View v) {
29 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
30 intent.setType("image/*");
31 intent.putExtra("crop", "true");// crop=true 有这句才能出来最后的裁剪页面.
32
33 intent.putExtra("aspectX", 1);// 这两项为裁剪框的比例.
34 intent.putExtra("aspectY", 2);// x:y=1:2
35
36 intent.putExtra("output", Uri.fromFile(tempFile));
37 intent.putExtra("outputFormat", "JPEG");//返回格式
38
39 startActivityForResult(Intent.createChooser(intent, "选择图片"), SELECT_PICTURE);
40 }
41 });
42 setContentView(button);
43 }
44
45 /**
46 * 裁剪完图片后系统调用的方法:onActivityResult
47 */
48 @Override
49 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
50 if (resultCode == RESULT_OK)
51 if (requestCode == SELECT_PICTURE)
52 button.setBackgroundDrawable(Drawable.createFromPath(tempFile.getAbsolutePath()));
53 }
54 }

  1. protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
  2. if(resultCode==RESULT_OK){
  3. switch(requestCode){
  4. caseREQ_CODE_CAMERA:
  5. uploadImage(photoTemp);
  6. break;
  7. caseREQ_CODE_PICTURES:
  8. Uriuri=data.getData();
  9. ContentResolvercr=this.getContentResolver();
  10. //getthephysicalpathoftheimage
  11. Cursorc=cr.query(uri,null,null,null,null);
  12. c.moveToFirst();
  13. photoTemp=c.getString(c.getColumnIndex("_data"));
  14. uploadImage(photoTemp);
  15. break;
  16. default:
  17. break;
  18. };
  19. }
  20. super.onActivityResult(requestCode,resultCode,data);
  21. }

更多相关文章

  1. Android(安卓)matrix 控制图片的旋转、缩放、移动
  2. Android(安卓)Wifi模块分析(三)
  3. Android中dispatchDraw分析
  4. Android四大基本组件介绍与生命周期
  5. android拍照与读取相册
  6. Android(安卓)报错:Caused by: android.os.FileUriExposedExcepti
  7. Android(安卓)Service AIDL
  8. Android调用天气预报的WebService简单例子
  9. android打电话发短信

随机推荐

  1. 一个c程序的执行是从什么开始到什么结束
  2. c语言源程序的扩展名是什么
  3. 7个华为关于C语言的经典面试题(分享)
  4. c语言fopen打开文件失败怎么办
  5. c语言中允许的基本数据类型包括哪些?
  6. 15道C语言开发面试题(原题分享)
  7. c语言程序格式是什么
  8. c语言有哪些合法关键字
  9. c++贪吃蛇代码是什么
  10. 经典C语言面试题(参考)