标签:
在Android开发中经常会用到图片的上传功能,一般常用的是调用Android系统自带的图库及拍照功能。示例代码如下:
Java代码:
public class TestActivity 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; private ImageView avatar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); avatar = (ImageView) findViewById(R.id.avatar); // 本地图库选择按钮 LinearLayout upload = (LinearLayout)findViewById(R.id.local_select_button); upload.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 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 OnClickListener() { @Override public void onClick(View v) { imageDir = "temp.jpg"; 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); 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) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream); avatar.setImageBitmap(photo); } } } super.onActivityResult(requestCode, resultCode, data); } }
布局文件:
<?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="@drawable/avatar_frame" /> <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="wrap_content" android:layout_height="wrap_content" android:background="@drawable/local" /> <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="wrap_content" android:layout_height="wrap_content" android:background="@drawable/take_photo" /> <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> </LinearLayout>
布局文件效果图如下:
本地选择效果图如下:
相机拍照效果如下:
图片缩放效果图:
图片上传结果效果图:
标签:
原文地址:http://www.cnblogs.com/a354823200/p/4283402.html