android 选择图片拍照并剪切照片上传到服务器

界面调用方法: pickerPicture 和 pickerPictureOk 为父类方法,

父类方法中调用上传,界面中只需要处理这两个方法即可;

技术分享


父类中处理打开照片,选择完成 和上传到服务器 调用 上传完成通知界面

技术分享


技术分享

选择图片并剪切,4.4的手机不同于其他

package com.idonoo.shareCar.ui.commom.activitys;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.idonoo.frame.model.ImageStorage;
import com.idonoo.frame.utils.ImageStoreHelper;
import com.idonoo.frame.utils.ImageUtil;
import com.idonoo.shareCar.R;
import com.idonoo.shareCar.app.IntentExtra;
import com.idonoo.shareCar.app.IntentResult;
import com.idonoo.shareCar.uiframe.BaseActivity;
import com.idonoo.shareCar.utils.PhotoUtility;
import com.intbird.soft.utils.BitmapHelper;
import com.intbird.soft.utils.CacheHelper;
import com.intbird.soft.utils.PhoneHelper;

public class TakeImageActivity extends BaseActivity {

	private int viewId;
	ImageStoreHelper.ImageOptions imageOptions;
	private ImageStorage imageFile;
	
	private String orignalFileUrl="";
	private String cutedFileUrl="";
	private String tempNewFilename="";
	
	private CopyImageTask task;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_popup_take_image);
		initUI();
		initData();
		CacheHelper.initCache("Idonoo");//actually only need once ,but i used when need it;
	}

	@Override
	protected void initUI() {
		super.initUI();
		super.initActionBar();

		setViewClickListener(listener,
				R.id.btn_select,
				R.id.btn_take_photo,
				R.id.cancel);
	}

	@Override
	protected void initData() {
		viewId=getIntent().getIntExtra(IntentExtra.EXTRA_IMAGESOTRE_VIEW_ID, 0);
		imageFile=(ImageStorage) getIntent().getSerializableExtra(IntentExtra.EXTRA_IMAGESOTRE_STORE_FILE);
		
		orignalFileUrl=imageFile.getFilePath();
		imageOptions=imageFile.getOptions();
		setSheetView();
	}

	private void setSheetView() {
		int sheetText=getIntent().getIntExtra(IntentExtra.EXTRA_IMAGESOTRE_SHEET_TEXT, 0);
		
		if(sheetText!=0){
			TextView tvTip = (TextView) findViewById(R.id.tv_tip_text);
			tvTip.setText(getString(sheetText));
			tvTip.setVisibility(View.VISIBLE);
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("orignalFileUrl", orignalFileUrl);
		outState.putString("cutedFileUrl", cutedFileUrl);
		outState.putString("tempNewFilename", tempNewFilename);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		orignalFileUrl=savedInstanceState.getString("orignalFileUrl");
		cutedFileUrl=savedInstanceState.getString("cutedFileUrl");
		tempNewFilename=savedInstanceState.getString("tempNewFilename");
	}

	View.OnClickListener listener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.btn_select: {
				try{
					take();
				}catch(Exception ex){
					showToast("打开手机拍照失败\n请尝试从相册选择~");
				}
			}
				break;
			case R.id.btn_take_photo: {
				try{
					showToast("请从系统图库中选择照片哦~");
					select();
				}catch(Exception ex){
					showToast("从相册选择失败\n请使用其他选图软件哦~");
				}
			}
				break;
			case R.id.cancel: {
				finish(false);
			}
				break;
			}

		}
	};

	private void take() {
		tempNewFilename=imageFile.getFileName()+System.currentTimeMillis()+".jpg";
		orignalFileUrl=CacheHelper.createCameraFile(tempNewFilename).getAbsolutePath();
		Intent intent = PhotoUtility.gotoCamera(orignalFileUrl);
		startActivityForResult(intent, IntentResult.RS_CAMERA);
	}

	private void select() {
		Intent intent = PhotoUtility.gotoGallery();
		if(PhoneHelper.isVersionHight(19)){
			intent=PhotoUtility.gotoGalleryPick();
		}
		startActivityForResult(intent, IntentResult.RS_GALLERY);
	}
	
	private void crop(){
		tempNewFilename=imageFile.getFileName()+System.currentTimeMillis()+".jpg";
		cutedFileUrl=CacheHelper.createImageFile(tempNewFilename).getAbsolutePath();
		try{
			Intent intent = PhotoUtility.cropImageFile(orignalFileUrl,cutedFileUrl,imageOptions.cropWidth,imageOptions.cropHeight);
			startActivityForResult(intent, IntentResult.RS_CROP);
		}catch(Exception ex){
			showToast("手机中缺少图片剪切程序\n将压缩上传原图,请保证图片大小合适");
			cutedFileUrl=orignalFileUrl;
			task=new CopyImageTask(imageOptions);
			task.execute();
		}
	}

	private void cropOk() {
        task=new CopyImageTask(imageOptions);
		task.execute();
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case IntentResult.RS_CAMERA: {
				crop();
			}
				break;
			case IntentResult.RS_GALLERY: {
				String tempGetUrl="";
				try{
					tempGetUrl = ImageUtil.getPathFromUri(data.getData());
				}catch(Exception ex){
					tempGetUrl="";
					showToast("貌似选择的图片出了点问题\n请重新选取图片");
				}
				if(TextUtils.isEmpty(tempGetUrl)){
					showToast("貌似选择的图片出了点问题\n请重新选取图片");
					return ;
				}
				orignalFileUrl = tempGetUrl;
				crop();
			}
				break;
			case IntentResult.RS_CROP: {
				cropOk();
			}
				break;
			}
		}
	}
	private class CopyImageTask extends AsyncTask<Void, Void, File>{
		private ImageStoreHelper.ImageOptions options;
		
		public CopyImageTask(ImageStoreHelper.ImageOptions imageOptions){
			this.options=imageOptions;
			showProgress("正在压缩图片");
		}
		@Override
		protected File doInBackground(Void... params) {
			Bitmap	croped = BitmapHelper.getBitmapFromFile(cutedFileUrl, options.cropWidth,options.cropHeight);
			//如果经过剪切后的图片大小为空,说明剪切失败了,则尝试获取原图
			if(croped==null){
				croped = BitmapHelper.getBitmapFromFile(orignalFileUrl, options.cropWidth,options.cropHeight);
			}
			
			//如果截取原图失败,将原图地址返回
			File newFile;
			if(croped==null){ 
				newFile=new File(orignalFileUrl);
				return newFile;
			}
			Bitmap bitmap = BitmapHelper.compressBitmap(croped, options.maxKb);
			newFile = CacheHelper.copyBitmapToCache(bitmap,tempNewFilename);
		
			return newFile;
		}
	

		@Override
		protected void onPostExecute(File file) {
			super.onPostExecute(file);
			dismissProgress();
		
			if(file==null||!file.exists()){
				showToast("复制选中文件到Idonoo文件夹下失败!\n检查图片或内存卡是否可用~");
				return ;
			}
			
			if(file.length()<10*1024){
				file.delete();
				showToast("上传图片小于10KB,请重新截取大图~");
				return ;
			}
			
			imageFile.setFilePath(file.getAbsolutePath());
			finish(true);
		}
	}
	
	private void finish(boolean isOk){
		if(!isOk) viewId=0;
		Intent intent=new Intent();
		intent.putExtra(IntentExtra.EXTRA_IMAGESOTRE_VIEW_ID, viewId);
		intent.putExtra(IntentExtra.EXTRA_IMAGESOTRE_STORE_FILE, imageFile);
		setResult(Activity.RESULT_OK,intent);
		
		if(task!=null&&!task.isCancelled())
			task.cancel(true);
		
		finish();
	}
}

package com.idonoo.shareCar.utils;


import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;

public class PhotoUtility {

    public static Intent gotoGallery() throws NullPointerException{
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); //ACTION_PICK//ACTION_GET_CONTENT
        intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        return intent;
    }
    
    public static Intent gotoGalleryPick() throws NullPointerException{
        Intent intent = new Intent(Intent.ACTION_PICK, null); //ACTION_PICK//ACTION_GET_CONTENT
        intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        return intent;
    }

    public static Intent gotoCamera(String filePath) throws NullPointerException{
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
        return intent;
    }

    public static Intent cropImage(Uri uri, String filePath, int width, int height) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", width);
        intent.putExtra("aspectY", height);
        intent.putExtra("outputX", width);
        intent.putExtra("outputY", height);
        intent.putExtra("return-data", false);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("scale", true); 
        intent.putExtra("scaleUpIfNeeded", true); 
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
        return intent;
    }
    

    
    public static Intent cropImage(String filePath, int width, int height) {
    	Uri uri=Uri.fromFile(new File(filePath));
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", width);
        intent.putExtra("aspectY", height);
        intent.putExtra("outputX", width);
        intent.putExtra("outputY", height);
        intent.putExtra("return-data", false);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("scale", true); 
        intent.putExtra("scaleUpIfNeeded", true); 
        intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        return intent;
    }
    
    public static Intent cropImageFile(String oldPath,String filePath,int width, int height) {
        Intent intent = new Intent("com.android.camera.action.CROP");    
        intent.setDataAndType(Uri.fromFile(new File(oldPath)), "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", width);
        intent.putExtra("aspectY", height);
        intent.putExtra("outputX", width);
        intent.putExtra("outputY", height);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("return-data", false);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("scale", true); 
        intent.putExtra("scaleUpIfNeeded", true); 
        intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(filePath)));
        return intent;
    }
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。