publicclassChoosePhotosActivity extends BaseActivity {
GridViewgridView;
ImageBucketAdapteradapter;// 自定义的适配器
private ArrayList<String>list;
privateArrayList<Map<String, String>> list1;
private Map<String,String> map;
private Map<String,String> map1;
publicstaticfinal String EXTRA_IMAGE_LIST= "agelist";
publicstatic Bitmap bimap;
privateintc_img[] = { R.drawable.im1, R.drawable.im2, R.drawable.im3,
R.drawable.im4, R.drawable.im5, R.drawable.im6, R.drawable.im7,
R.drawable.im8, R.drawable.im9, R.drawable.im10, R.drawable.im11,
R.drawable.im12, R.drawable.im13 };
private HashMap<String,Integer> map2;
private HashMap<String,String> map3;
private sql_execute sql_execute;
private List<Execute> list_execute;
private ArrayList<String>list_Position;
private String file;
private TextView mTVUpdatePhoto;
private TextView mTvActionBarCenter;
private TextView mIvActionBarRight;
private ImageView mIvActionBarLeft;
private HelpMsgModel hmm;
private String serverType;
private String userCarNum;
private String userCarType;
private String insurer;
private String userPhone;
private String taskId;
private String address;
private String userName;
private String Other;
private ProgressDialog loadingDialog = null;
private String service_text;
privateintserviceId=0;
privatestaticfinalintTAKE_PICTURE = 0;
privatestaticfinalintCHOOSE_PICTURE= 1;
privatestaticintPosition=0;
@Override
protectedvoid onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_image_bucket);
initData();
initDb();
contentData();
clectPhoto();
initView();
map = list1.get(serviceId);
map1 =newHashMap<String,String>();
map2 =newHashMap<String,Integer>();
map3 =newHashMap<String,String>();
for(inti=0;i<map.size();i++){
String aa = getTextTitle(map.get(i+""));
map1.put(i+"",aa);
map2.put(i+"",c_img[Integer.valueOf(map.get(i+""))-1]);
}
loadingDialog = newProgressDialog(ChoosePhotosActivity.this);
loadingDialog.setMessage("正在加载...");
loadingDialog.show();
new MyThread2().start();
}
privatevoid initData() {
hmm = (HelpMsgModel) getIntent().getExtras().get("HMM");
serverType =hmm.getServerType();
userCarNum =hmm.getUserCarNum();
userCarType =hmm.getUserCarType();
insurer =hmm.getInsurer();
userPhone =hmm.getUserPhone();
taskId =hmm.getTaskId();
address =hmm.getAddress();
userName =hmm.getUserName();
Other =hmm.getOther();
service_text =hmm.getServerType();
}
privatevoid initDb() {
sql_execute =newsql_execute(getApplicationContext());
}
public String getTextTitle(String str){
int ii =Integer.valueOf(str);
returnlist.get(ii-1);
}
/**
* 初始化view视图
*/
privatevoid initView() {
mTvActionBarCenter=(TextView)this.findViewById(R.id.mTvActionBarCenter);
mTvActionBarCenter.setText("拍摄现场照片");
mIvActionBarRight=(TextView)this.findViewById(R.id.mIvActionBarRight);
mIvActionBarRight.setText("完成");
mIvActionBarRight.setOnClickListener(listener);
mIvActionBarLeft =(ImageView)this.findViewById(R.id.mIvActionBarLeft);
mIvActionBarLeft.setOnClickListener(listener);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
}
OnClickListenerlistener=newView.OnClickListener() {
@Override
publicvoid onClick(View v) {
switch (v.getId()) {
case R.id.mIvActionBarRight:
loadingDialog = newProgressDialog(ChoosePhotosActivity.this);
loadingDialog.setMessage("正在加载...");
loadingDialog.show();
new MyThread1().start();
break;
case R.id.mIvActionBarLeft:
AppManager.getAppManager().finishActivity();
break;
default:
break;
}
}
};
privatevoid culcateHasPosition(){
list_Position =newArrayList<String>();
//说明默认的图片的所有的Position已经填满了
if(list_execute.size()>=map1.size()){
for(inti=0;i<list_execute.size();i++){
list_Position.add(i+"");
}
}
for(inti=0;i<list_execute.size();i++){
if(list_execute.get(0).getPhotoPosition()==i+""){
}
}
}
private Handler handler =new MyHandler();
class MyHandler extends Handler{
@Override
publicvoid handleMessage(Messagemsg) {
switch (msg.what) {
case 1:
adapter =newImageBucketAdapter(ChoosePhotosActivity.this, map1, map2,map3,list_execute,handler);
//将处理过的图片显示在界面上,并保存到本地
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
publicvoidonItemClick(AdapterView<?> parent, View view,
int position, long id) {
//按了+号
if(position==(gridView.getCount()-1)){
//前面的已经输完了
if(list_execute.size()>=position){
Position = (int)position;
// 调用系统的拍照功能
Intentintent2 = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
FiletempFile = newFile(Environment.getExternalStorageDirectory()
+"/CyyDriver/", getPhotoFileName());
// 指定调用相机拍照后照片的储存路径
intent2.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));
startActivityForResult(intent2,2);
}else{
Toast.makeText(getApplicationContext(),"请将上面的图片先拍完", 0).show();
}
}else{
boolean flag =false;
for(int i=0;i<list_execute.size();i++){
if(list_execute.get(i).getPhotoPosition().equals(position+"")){
flag=true;
}}
//如果有此值
if(!flag){
Position = position;
Intentintent1 =newIntent(getApplicationContext(), ActivityCenterHelp.class);
intent1.putExtra("Position",Position);
intent1.putExtra("serviceId",serviceId);
startActivityForResult(intent1,TAKE_PICTURE);
}else{
Position = position;
Intentintent3 =newIntent(getApplicationContext(), ActivityPhotoGraphy.class);
intent3.putExtra("PATH", list_execute.get(Position).getPhotoPath());
intent3.putExtra("POSITION",Position);
startActivityForResult(intent3,TAKE_PICTURE);
}
}
}
});
adapter.notifyDataSetChanged();
if(loadingDialog.isShowing()){
loadingDialog.cancel();
}
break;
case 2:
Position = (int) msg.obj;
Intentintent1 =newIntent(getApplicationContext(), ActivityCenterHelp.class);
intent1.putExtra("Position",Position);
intent1.putExtra("serviceId",serviceId);
startActivityForResult(intent1,TAKE_PICTURE);
break;
case 3:
Position = (int) msg.obj;
// 调用系统的拍照功能
Intentintent2 = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
FiletempFile = newFile(Environment.getExternalStorageDirectory()
+"/CyyDriver/", getPhotoFileName());
// 指定调用相机拍照后照片的储存路径
intent2.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));
startActivityForResult(intent2,2);
break;
case 4:
Toast.makeText(getApplicationContext(),"请将上面的图片先拍完", 0).show();
break;
case 5:
Position = (int) msg.obj;
Intentintent3 =newIntent(getApplicationContext(), ActivityPhotoGraphy.class);
intent3.putExtra("PATH", list_execute.get(Position).getPhotoPath());
intent3.putExtra("POSITION",Position);
startActivityForResult(intent3,TAKE_PICTURE);
break;
case 11:
if(loadingDialog.isShowing()){
loadingDialog.cancel();
}
Toast.makeText(ChoosePhotosActivity.this, "请完成指定需要拍摄的图片", 1).show();
break;
case 12:
if(loadingDialog.isShowing()){
loadingDialog.cancel();
}
AppManager.getAppManager().finishActivity();
break;
default:
break;
}
super.handleMessage(msg);
}
}
//点击完成时对于数据的处理
privateclass MyThread2 extends Thread {
@Override
publicvoid run() {
list_execute = sql_execute.query(taskId, "0");
handler.sendEmptyMessage(1);
super.run();
}
}
//点击完成时对于数据的处理
privateclass MyThread1 extends Thread {
@Override
publicvoid run() {
int a = sql_execute.querycount(taskId, "0");
if(a<map1.size()){
handler.sendEmptyMessage(11);
}else{
sql_execute exe =newsql_execute(getApplicationContext());
TaskMap tp =new TaskMap();
tp.setIsCamera("1");
tp.setStatus("0");
tp.setTaskId(taskId);
exe.insert(tp);
handler.sendEmptyMessage(12);
}
super.run();
}
}
//拍照时处理的后台逻辑
privateclass MyThread extends Thread{
privateintrequestCode;
privateintresultCode;
private Intent data;
public MyThread(int requestCode, int resultCode, Intentdata){
this.requestCode =requestCode;
this.resultCode =resultCode;
this.data =data;
}
@Override
publicvoid run() {
if (resultCode == RESULT_OK) {
Stringpath;
switch (requestCode) {
case 2:
Datedate = newDate(System.currentTimeMillis());
SimpleDateFormatformatter = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
Executeexe =newExecute();
exe.setPhotoPath(Environment.getExternalStorageDirectory()+"/CyyDriver/"+file);
exe.setTaskId(taskId);
exe.setPhotoPosition(Position+"");
//holder.name.setText((position+1)+"、"+map1.get(position+""));
exe.setPhotoBucketName((Position+1)+"、"+map1.get(Position+""));
exe.setPhotoObjectKey("");
exe.setPhotoSignatual(0);
exe.setPhotoTime(dateString);
exe.setIsUpload(0);
sql_execute.insert(exe);
list_execute = sql_execute.query(taskId, "0");
map3 = newHashMap<String,String>();
map3.put("POSITION",Position+"");
map3.put("FILE", file);
PhotoModelpm =newPhotoModel();
pm.setPhotoPath(file);
pm.setPosition(Position+"");
handler.sendEmptyMessage(1);
break;
default:
break;
}
}elseif(resultCode ==4){
//将保存在本地的图片取出并缩小后显示在界面上
String tempFile1 = data.getStringExtra("FILE");
Datedate = newDate(System.currentTimeMillis());
SimpleDateFormatformatter = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
Executeexe =newExecute();
exe.setPhotoPath(Environment.getExternalStorageDirectory()+"/CyyDriver/"+tempFile1);
exe.setTaskId(taskId);
exe.setPhotoPosition((Position)+"");
exe.setPhotoBucketName((Position+1)+"、"+map1.get(Position+""));
exe.setPhotoObjectKey("");
exe.setPhotoSignatual(0);
exe.setPhotoTime(dateString);
exe.setIsUpload(0);
sql_execute.insert(exe);
list_execute = sql_execute.query(taskId, "0");
map3 = newHashMap<String,String>();
map3.put("POSITION",Position+"");
map3.put("FILE", tempFile1);
PhotoModelpm =newPhotoModel();
pm.setPhotoPath(tempFile1);
pm.setPosition(Position+"");
handler.sendEmptyMessage(1);
}
}
}
@Override
protectedvoid onActivityResult(int requestCode, int resultCode, Intentdata) {
super.onActivityResult(requestCode,resultCode, data);
loadingDialog = new ProgressDialog(ChoosePhotosActivity.this);
loadingDialog.setMessage("正在加载...");
loadingDialog.show();
new MyThread(requestCode, resultCode,data).start();
}
FiletempFile= newFile(Environment.getExternalStorageDirectory()
+"/CyyDriver/", getPhotoFileName());
// 使用系统当前日期加以调整作为照片的名称
private StringgetPhotoFileName() {
Datedate = newDate(System.currentTimeMillis());
SimpleDateFormatdateFormat = newSimpleDateFormat(
"‘IMG‘_yyyyMMdd_HHmmss");
file=dateFormat.format(date) + ".jpg";
return dateFormat.format(date)+ ".jpg";
}
@Override
protectedvoid onResume() {
if(loadingDialog.isShowing()){
loadingDialog.cancel();
}
super.onResume();
}
}
图片适配器:
publicclassImageBucketAdapter extends BaseAdapter {
final String TAG =getClass().getSimpleName();
//最上面显示的文字
Map<String,String> map1;
//中间显示的图片
Map<String,Integer> map2;
//用来刷新拍照的照片
Map<String,String> map3;
Activityact;
List<Execute>list;
BitmapCachecache;
private File tempFile;
private Integer itemsPosition;
privateintsize =0;
private Handler handler;
public ImageBucketAdapter(Activityact,Map<String, String> map1,Map<String, Integer>map2,Map<String, String> map3,List<Execute> list,Handler handler) {
this.act = act;
cache = new BitmapCache();
this.map1 =map1;
this.map2 =map2;
this.map3 =map3;
this.list =list;
this.handler =handler;
}
@Override
publicint getCount() {
if(list.size()>=map1.size()){
size=list.size()+1;
}else{
size =map1.size()+1;
}
returnsize;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
publiclong getItemId(int position) {
return position;
}
class Holder {
private ImageView iv;
private TextView name;
private TextView mTVTimePhotoNow,mTVNoteFouse,mTVPhotoNow;
}
@Override
public View getView(finalint position, View view,ViewGroup arg2) {
// TODO Auto-generated method stub
Holderholder;
if (view == null) {
holder= newHolder();
view= View.inflate(act, R.layout.testitem_image_bucket, null);
holder.iv = (ImageView)view.findViewById(R.id.image);
holder.name = (TextView)view.findViewById(R.id.name);
holder.mTVTimePhotoNow=(TextView)view.findViewById(R.id.mTVTimePhotoNow);
holder.mTVNoteFouse=(TextView)view.findViewById(R.id.mTVNoteFouse);
holder.mTVPhotoNow=(TextView)view.findViewById(R.id.mTVPhotoNow);
view.setTag(holder);
}else{
holder= (Holder) view.getTag();
}
if(position<map1.size()){
holder.name.setText((position+1)+"、"+map1.get(position+""));
holder.iv.setImageResource(map2.get(position+""));
holder.mTVPhotoNow.setVisibility(View.VISIBLE);
holder.mTVNoteFouse.setVisibility(View.GONE);
holder.mTVTimePhotoNow.setVisibility(View.GONE);
}else{
holder.name.setText("");
holder.mTVPhotoNow.setVisibility(View.GONE);
holder.mTVNoteFouse.setVisibility(View.VISIBLE);
holder.mTVTimePhotoNow.setVisibility(View.VISIBLE);
}
BitmapDrawablebitmapDrawable = (BitmapDrawable) holder.iv.getDrawable();
//如果图片还未回收,先强制回收该图片
if(bitmapDrawable.getBitmap().isRecycled())
{
bitmapDrawable.getBitmap().recycle();
}
for(int i=0;i<list.size();i++){
if(list.get(i).getPhotoPosition().equals(position+"")){
tempFile = new File(list.get(i).getPhotoPath());
if(tempFile.exists()){
Bitmapbitmap = BitmapFactory.decodeFile(tempFile.getPath());
BitmapnewBitmap = ImageTools.zoomBitmap(bitmap, 156,128);
//由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常
bitmap.recycle();
holder.iv.setImageBitmap(newBitmap);
holder.mTVPhotoNow.setVisibility(View.GONE);
holder.mTVNoteFouse.setVisibility(View.VISIBLE);
holder.mTVTimePhotoNow.setVisibility(View.VISIBLE);
holder.mTVTimePhotoNow.setText(list.get(position).getPhotoTime());
}
}
}
//当position显示到最后一个时显示一个+号
if(position==(size-1)){
holder.iv.setImageResource(R.drawable.addpic_nor);
holder.mTVPhotoNow.setVisibility(View.GONE);
holder.mTVNoteFouse.setVisibility(View.GONE);
holder.mTVTimePhotoNow.setVisibility(View.GONE);
//holder.iv.setClickable(true);
}
}
对于图片的Db操作略,