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
- android截取图片
- Android图片截取
- Android BitmapRegionDecoder截取图片
- android之图片截取
- Android截取圆角图片
- Android中拍照截取和相册获取图片截取
- android截取当前view为图片
- Android如何截取当前View 为图片
- Android中截取当前屏幕图片
- android实现图片按任意路径截取
- android 图片处理(截取,缩放)
- android图片截取的两种方式
- android 图片处理(截取,缩放)
- Android 图片截取人物头像(仿逗拍)
- android 图片处理(截取,缩放)
- Android第三方开源图片裁剪截取:cropper
- android图片截取的两种形式
- android 图片处理(截取,缩放)
- java中HashMap详解
- 解决ora-01652无法通过128(在temp表空间中)扩展temp段的过程
- TestNG - 添加图片或者链接到测试报告
- Javascript 刷新框架及页面的方法总集
- spark内核揭秘-08-spark的Web监控页面
- android截取图片
- hdu1018 big number
- 解决Maven中使用很多本地jar包的编译问题
- 老外看中国:本土移动应用差异在哪?
- POJ 1324 Holedox Moving 贪吃蛇 状态压缩 BFS
- DNS、DNS Proxy配置要点及实例
- eclipse中双击一个变量,文件中所有这个变量都会变成黄色,现在变不了了的问题。
- 中小企业如何选择网络营销平台
- VMware mount共享文件夹到指定位置