拍照
view source print ?
02 | startCamera.setOnClickListener( new OnClickListener() { |
05 | public void onClick(View v) { |
06 | // TODO Auto-generated method stub |
08 | // mCurrentPhotoFile = new File(PHOTO_DIR,getPhotoFileName()); |
10 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
11 | intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1 ); |
12 | // Intent intent = new |
13 | // Intent("android.media.action.IMAGE_CAPTURE"); |
14 | // Uri fromFile = Uri.fromFile(mCurrentPhotoFile); |
15 | // String string = fromFile.toString(); |
16 | // Log.i("gp", string); |
17 | // intent.putExtra(MediaStore.EXTRA_OUTPUT, |
18 | // Uri.fromFile(mCurrentPhotoFile)); |
19 | startActivityForResult(intent, CAPTURE_CODE); |
调用系统相册
view source print ?
02 | camera_look_photo.setOnClickListener( new OnClickListener() { |
05 | public void onClick(View v) { |
06 | // TODO Auto-generated method stub |
07 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
08 | intent.setType( "image/*" ); |
09 | startActivityForResult(intent, IMAGE_CODE); |
对回调函数进行处理
view source print ?
001 | protected void onActivityResult( int requestCode, int resultCode, Intent data) { |
002 | // TODO Auto-generated method stub |
003 | super .onActivityResult(requestCode, resultCode, data); |
004 | ContentResolver resolver = getContentResolver(); |
007 | if (resultCode != RESULT_OK) |
009 | switch (requestCode) { |
011 | // Toast.makeText(Camera.this, "--------------", 2).show(); |
013 | Bitmap set_map = null ; |
016 | // doCropPhoto(mCurrentPhotoFile); |
017 | String sdStatus = Environment.getExternalStorageState(); |
018 | Bundle bundle = data.getExtras(); |
019 | if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { |
020 | bitMap = (Bitmap) data.getParcelableExtra( "data" ); |
021 | if (bitMap == null ) { |
022 | Uri originalUri = data.getData(); |
025 | .println(Build.MODEL+ "----------camera----------------------611-------" |
026 | + originalUri.toString()); |
027 | byte [] mContent = readStream(resolver |
028 | .openInputStream(Uri.parse(originalUri |
030 | if (bitMap!= null )bitMap.recycle(); |
031 | bitMap = getPicFromBytes(mContent, null ); |
032 | cam_photo.setImageBitmap(bitMap); |
034 | camera_paht = "/data/sdcard/DCIM/Camera/" + getPhotoFileName(); |
035 | File aFile = new File( "/data/sdcard/DCIM/Camera/" ); // 图片上传地址 |
038 | System.out.println( "camera-----------------------630------------创建文件---" ); |
041 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
042 | if (set_map!= null )set_map.recycle(); |
043 | set_map = scaleImg(bitMap, x[position_s], |
045 | set_map.compress(Bitmap.CompressFormat.JPEG, 25 , out); |
046 | photo = out.toByteArray(); |
049 | FileOutputStream fos = new FileOutputStream(camera_paht); // 文件输出流 |
054 | } catch (IOException e) { |
055 | // TODO Auto-generated catch block |
059 | } catch (FileNotFoundException e) { |
060 | // TODO Auto-generated catch block |
064 | } catch (FileNotFoundException e) { |
065 | // TODO Auto-generated catch block |
067 | } catch (Exception e) { |
068 | // TODO Auto-generated catch block |
073 | //bitMap = (Bitmap) bundle.get("data"); |
074 | Bitmap set_map2 = null ; |
075 | bitMap = (Bitmap) data.getParcelableExtra( "data" ); |
076 | System.out.println(Build.MODEL+ "camera----------------660-------" +bitMap); |
077 | if (bitMap == null ) { |
078 | Uri originalUri = data.getData(); |
079 | System.out.println( "camera----------------------687-------" |
080 | + originalUri.toString()); |
081 | byte [] mContent= null ; |
083 | mContent = readStream(resolver.openInputStream(Uri |
084 | .parse(originalUri.toString()))); |
085 | if (bitMap!= null )bitMap.recycle(); |
086 | bitMap = getPicFromBytes(mContent, null ); |
087 | } catch (Exception e1) { |
088 | // TODO Auto-generated catch block |
089 | e1.printStackTrace(); |
094 | Bitmap resizedBitmap = null ; |
095 | if (Build.MODEL.equals( "7260" )){ |
096 | Matrix matrix = new Matrix(); |
097 | matrix.postRotate( 270 ); |
098 | resizedBitmap = Bitmap.createBitmap(bitMap, , ,bitMap.getWidth(), bitMap.getHeight(), matrix, true ); |
099 | bitMap = resizedBitmap; |
102 | .println( "camera------------------585----------bitmap---" |
104 | // map = scaleImg(bitMap, 250, 420); |
105 | System.out.println( "Camera--------------75------" ); |
107 | cam_photo.setImageBitmap(bitMap); |
108 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
110 | if (set_map2!= null )set_map2.recycle(); |
111 | set_map2 = scaleImg(bitMap, x[position_s], |
113 | if (set_map2== null ) return ; |
115 | set_map2.compress(Bitmap.CompressFormat.JPEG, 25 , out); |
116 | photo = out.toByteArray(); |
119 | } catch (IOException e1) { |
120 | // TODO Auto-generated catch block |
121 | e1.printStackTrace(); |
123 | intTobyte(photo.length); // 图片的长度 |
124 | System.out.println( "camera-----------------------722-------" +photo_len); |
125 | camera_paht = PHOTO_DIR + File.separator |
126 | + getPhotoFileName(); |
128 | .println( "camera ---------照片存储地址----------419-----" |
130 | File aFile = new File(camera_paht); // 图片上传地址 |
132 | FileOutputStream fos = new FileOutputStream(aFile); // 文件输出流 |
137 | } catch (IOException e) { |
138 | // TODO Auto-generated catch block |
142 | } catch (FileNotFoundException e) { |
143 | // TODO Auto-generated catch block |
146 | //System.out.println("-----photo----" + photo.length); |
147 | // Toast.makeText(Camera.this, "" + photo.length, 2).show(); |
154 | // camera_paht =PHOTO_DIR+File.separator+getPhotoFileName() ; |
155 | Bundle bundle = data.getExtras(); |
156 | System.out.println( "Camera--------------75------" ); |
159 | Uri originalUri = data.getData(); |
160 | if (originalUri != null ) { |
161 | Bitmap bitMap = null ; |
163 | if (bitMap!= null )bitMap.recycle(); |
165 | bitMap = MediaStore.Images.Media.getBitmap(resolver, |
167 | String[] proj = { MediaColumns.DATA }; |
168 | Cursor cursor = managedQuery(originalUri, proj, null , null , |
170 | // 按我个人理解 这个是获得用户选择的图片的索引值 |
171 | int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); |
172 | System.out.println( "706-------------camera------" |
174 | // 将光标移至开头 ,这个很重要,不小心很容易引起越界 |
175 | cursor.moveToFirst(); |
178 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
179 | uploadPhotPath = cursor.getString(column_index); |
181 | .println( "camera--------------------561----------- 相册图片的地址--------" |
183 | Bitmap map = scaleImg(bitMap, 250 , 420 ); |
184 | map.compress(Bitmap.CompressFormat.JPEG, 25 , out); |
185 | photo = out.toByteArray(); |
186 | intTobyte(photo.length); // 图片的长度 |
188 | .println( "camera-----------------565----------相册图片的长度-----" |
190 | cam_photo.setImageBitmap(map); |
191 | } catch (Exception e) { |
192 | Toast.makeText(Camera. this , "图片不可用" , 3 ).show(); |
图片的上传,以前是用特定的协议进行上传,不知道是协议的什么地方出现了,一直上传不成功。在网上找到另一种上传方法,Http有自己封装好的方法,
view source print ?
01 | private void uploadFile2Svr2() { |
03 | file = new File(camera_paht); |
07 | if (uploadPhotPath== null ) |
09 | file = new File(uploadPhotPath); |
12 | System.out.println( "camera------------------------807" ); |
15 | String httpUrl = path; |
16 | System.out.println( "---------------" ); |
17 | HttpPost request = new HttpPost(httpUrl); |
18 | HttpClient httpClient = new DefaultHttpClient(); |
19 | FileEntity entity = new FileEntity(file, "binary/octet-stream" ); |
20 | HttpResponse response; |
21 | request.setEntity(entity); |
22 | entity.setContentEncoding( "binary/octet-stream" ); |
24 | response = httpClient.execute(request); |
25 | System.out.println(response.toString()); |
26 | } catch (ClientProtocolException e) { |
27 | Toast.makeText(Camera. this , "图片不可用,请重新拍摄上传" , 3 ).show(); |
28 | // TODO Auto-generated catch block |
30 | } catch (IOException e) { |
31 | // TODO Auto-generated catch block |
32 | Toast.makeText(Camera. this , "图片不可用,请重新拍摄上传" , 3 ).show(); |