android拍照选择图片上传服务器自定义控件
做android项目的时候总免不了遇到图片上传功能,虽然就是调用android系统的拍照和相册选择功能,但是总面部了把一大推代码写在activity里,看上去一大推代码头都昏了。不如把这些功能都集成一个控件,以后遇到图片上传功能也不用那么麻烦了。好啦,下面开始上效果图。
</pre><pre name="code" class="java" style="font-size: 11.8181819915771px;">
</pre><pre name="code" class="java" style="font-size: 11.8181819915771px;">
效果图就是这样了,我们看效果图可以分析出大致的功能就是:一个Imageview里默认有一张图片,我们在选择了图片以后右上角有个删除的Imageview,点击以后可以删除选择的图片,那实际上就是一个FrameLayout嘛,好,下面开始贴代码。基本看代码都能看懂了,需要注意的是做了一下图片压缩的处理。因为不做压缩的话一个图片拍下来都是几兆大小,上传完全不可行。
</pre><pre name="code" class="java" style="font-size: 11.8181819915771px;">自定义控件代码
</pre><pre name="code" class="java">
public class ImagePicker extends FrameLayout implements View.OnClickListener { private SelectType selectType; //选择的类型照相和选择 private Context context; private ImageView imageView; private ImageView deleteImageView; private int imageViewWidth; private int imageViewHeight; private boolean hasImage; private String imagePath; private File file; private LinearLayout dialogView; private AlertDialog dialog; private int scaleWidth=240; private int scaleHeight=240; public void setScale(int width,int height){ this.scaleWidth=width; this.scaleHeight=height; } public ImagePicker(Context context) { super(context); this.context=context; } public ImagePicker(Context context, AttributeSet attrs) { super(context, attrs); this.context=context; } public ImagePicker(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context=context; } public boolean hasImage(){ return this.hasImage; } public String getImagePath(){ return this.imagePath; } public void setImagePath(String path){ File file=new File(path); if(!file.exists()){ clearImagePicker(); return; } Bitmap bitmap=ImageUtils.getInstance().decodeSampledBitmap(file,imageViewWidth,imageViewHeight); if(bitmap==null){ clearImagePicker(); return; } int degree = ImageUtils.getInstance().readPicDegree(path); if(degree>0){ bitmap=ImageUtils.getInstance().rotateBitmap(degree,bitmap); } imageView.setImageBitmap(bitmap); imagePath=path; hasImage=true; deleteImageView.setVisibility(View.VISIBLE); } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK&& requestCode == getId()) { if(selectType==null){ selectType=SelectType.CAMERA; } switch (selectType){ case CAMERA: onCameraResult(data); break; case PHOTO: onPhotoResult(data); break; } } } private void onPhotoResult(Intent data){ Uri originalUri = data.getData(); Cursor cursor = ((Activity)context).managedQuery(originalUri, new String[]{MediaStore.Images.Media.DATA}, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); try { File tmpFile=compressImage(path); setImagePath(tmpFile.getPath()); } catch (IOException e) { e.printStackTrace(); Toast.makeText(context,"压缩图片失败",Toast.LENGTH_SHORT).show(); clearImagePicker(); return; } } private void onCameraResult(Intent data){ try { File tmpFile=compressImage(file.getPath()); setImagePath(tmpFile.getPath()); file.delete(); file=tmpFile; } catch (IOException e) { e.printStackTrace(); Toast.makeText(context,"压缩图片失败",Toast.LENGTH_SHORT).show(); clearImagePicker(); return; } } private File compressImage(String path) throws IOException { //图片翻转角度 int degree=ImageUtils.getInstance().readPicDegree(path); //缩放 BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true; BitmapFactory.decodeFile(path,newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; int be = 1; if (w > h && w > scaleWidth) { be = newOpts.outWidth / scaleWidth; } else if (w < h && h > scaleHeight) { be = newOpts.outHeight / scaleHeight; } if (be <= 0){ be = 1; } newOpts.inSampleSize = be; Bitmap bitmap = BitmapFactory.decodeFile(path, newOpts); if(degree>0){ bitmap=ImageUtils.getInstance().rotateBitmap(degree,bitmap); } //压缩 ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; while ( baos.toByteArray().length / 1024>100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩 options -= 10; baos.reset(); bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos); } //保存 String name = new DateFormat().format("yyyyMMddhhmmss", Calendar.getInstance()) + ".jpg"; File file = new File(SDCardUtils.getImageDir(context),name); FileOutputStream b = new FileOutputStream(file); baos.writeTo(b); baos.close(); b.close(); return file; } @Override protected void onFinishInflate() { super.onFinishInflate(); imageView=(ImageView)this.findViewById(R.id.ImagePicker_ImageView); deleteImageView=(ImageView)this.findViewById(R.id.ImagePicker_ImageDelete); imageView.setOnClickListener(this); deleteImageView.setOnClickListener(this); deleteImageView.setVisibility(View.GONE); dialogView=(LinearLayout)LayoutInflater.from(this.context).inflate(R.layout.image_picker_dialog_layout,null); dialogView.findViewById(R.id.ImagePicker_PhotoBtn).setOnClickListener(this); dialogView.findViewById(R.id.ImagePicker_CameraBtn).setOnClickListener(this); dialogView.findViewById(R.id.ImagePicker_CancelBtn).setOnClickListener(this); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); imageViewWidth=imageView.getWidth();; imageViewHeight=imageView.getHeight();; } @Override public void onClick(View v) { switch (v.getId()){ case R.id.ImagePicker_ImageView: showImagePickerDialog(); break; case R.id.ImagePicker_ImageDelete: clearImagePicker(); break; case R.id.ImagePicker_CameraBtn: showCameraApp(); break; case R.id.ImagePicker_PhotoBtn: showPhotoApp(); break; case R.id.ImagePicker_CancelBtn: closeDialog(); break; } } private void closeDialog(){ dialog.dismiss(); dialog=null; } private void showImagePickerDialog(){ ViewGroup parent=(ViewGroup)dialogView.getParent(); if(parent!=null){ parent.removeView(dialogView); } dialog= new AlertDialog.Builder(context).setTitle("图片选择") .setView(dialogView).create(); dialog.show(); } private void showPhotoApp(){ selectType=SelectType.PHOTO; Intent photoIntent= new Intent(Intent.ACTION_GET_CONTENT); photoIntent.setType("image/*"); ((Activity)context).startActivityForResult(photoIntent, getId()); closeDialog(); } private void showCameraApp(){ if(!SDCardUtils.hasSDCard()){ Toast.makeText(context,"请插入sd卡",Toast.LENGTH_SHORT).show(); return; } String name = new DateFormat().format("yyyyMMddhhmmss", Calendar.getInstance()) + ".jpg"; file = new File(SDCardUtils.getImageDir(context),name); selectType=SelectType.CAMERA; Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); ((Activity)context).startActivityForResult(cameraIntent, getId()); closeDialog(); } public void clearImagePicker(){ hasImage=false; imagePath=null; imageView.setImageResource(R.drawable.image_picker_add); deleteImageView.setVisibility(View.GONE); } enum SelectType{ PHOTO,CAMERA } }
</pre><p></p><p>我们的控件布局是这样的。</p><p></p><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <com.example.imageUpload.widget.ImagePicker xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="10dip" android:id="@+id/ImagePicker_ImageView" android:layout_gravity="center" android:src="@drawable/image_picker_add" android:scaleType="fitXY"/> <ImageView android:layout_width="20dip" android:layout_height="20dip" android:id="@+id/ImagePicker_ImageDelete" android:src="@drawable/image_picker_delete" android:layout_gravity="right|top" /> </com.example.imageUpload.widget.ImagePicker>
下面开始使用吧!在activity的xml文件里面include一下就可以了,布局代码我就不贴了,来看activity里面。我们需要重写一下onActivityResult,
上传文件我们无非就是用到 //imagePicker1.getImagePath();得到图片的路径
//imagePicker1.hasImage();是否有图片。
public class MyActivity extends Activity { private ImagePicker imagePicker1; private ImagePicker imagePicker2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imagePicker1 = (ImagePicker)findViewById(R.id.imagePicker1); imagePicker2 = (ImagePicker)findViewById(R.id.imagePicker2); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); imagePicker1.onActivityResult(requestCode, resultCode, data); imagePicker2.onActivityResult(requestCode,resultCode,data); //imagePicker1.getImagePath(); //imagePicker1.hasImage(); } }