android拍照,调用系统相册,相片上传

原创
2012/07/14 11:21
阅读数 2.6W

测试机为小米,酷派

拍照

// 启动相机
		startCamera.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				// mCurrentPhotoFile = new File(PHOTO_DIR,getPhotoFileName());
				// //用当前时间给取得的图片命名
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
				// Intent intent = new
				// Intent("android.media.action.IMAGE_CAPTURE");
				// Uri fromFile = Uri.fromFile(mCurrentPhotoFile);
				// String string = fromFile.toString();
				// Log.i("gp", string);
				// intent.putExtra(MediaStore.EXTRA_OUTPUT,
				// Uri.fromFile(mCurrentPhotoFile));
				startActivityForResult(intent, CAPTURE_CODE);

			}
		});

调用系统相册

// 查看相片
		camera_look_photo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.setType("image/*");
				startActivityForResult(intent, IMAGE_CODE);

			}
		});

对回调函数进行处理

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		ContentResolver resolver = getContentResolver();
		
		// 拍照
		if (resultCode != RESULT_OK)
			return;
		switch (requestCode) {
		case CAPTURE_CODE: {
			// Toast.makeText(Camera.this, "--------------", 2).show();
			
			Bitmap set_map = null;
			paizhao = true;
			xiangce = false;
			// doCropPhoto(mCurrentPhotoFile);
			String sdStatus = Environment.getExternalStorageState();
			Bundle bundle = data.getExtras();
			if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
				bitMap = (Bitmap) data.getParcelableExtra("data");
				if (bitMap == null) {
					Uri originalUri = data.getData();
					try {
						System.out
								.println(Build.MODEL+"----------camera----------------------611-------"
										+ originalUri.toString());
						byte[] mContent = readStream(resolver
								.openInputStream(Uri.parse(originalUri
										.toString())));
						if(bitMap!=null)bitMap.recycle();
						bitMap = getPicFromBytes(mContent, null);
						cam_photo.setImageBitmap(bitMap);
						
						camera_paht = "/data/sdcard/DCIM/Camera/"+ getPhotoFileName();
					File aFile = new File("/data/sdcard/DCIM/Camera/"); // 图片上传地址
					if(!aFile.exists())
					{
						System.out.println("camera-----------------------630------------创建文件---");
						aFile.mkdirs();
					}
						ByteArrayOutputStream out = new ByteArrayOutputStream();
						if(set_map!=null)set_map.recycle();
						set_map = scaleImg(bitMap, x[position_s],
								y[position_s]);
						set_map.compress(Bitmap.CompressFormat.JPEG, 25, out);
						photo = out.toByteArray();
						out.close();
						try {
							FileOutputStream fos = new FileOutputStream(camera_paht); // 文件输出流
							try {
								fos.write(photo);
							//	bitMap.recycle();
								fos.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else {
				//bitMap = (Bitmap) bundle.get("data");
				Bitmap set_map2 = null;
				bitMap = (Bitmap) data.getParcelableExtra("data");
				System.out.println(Build.MODEL+"camera----------------660-------"+bitMap);
				if (bitMap == null) {
					Uri originalUri = data.getData();
					System.out.println("camera----------------------687-------"
							+ originalUri.toString());
					byte[] mContent=null;
					try {
						mContent = readStream(resolver.openInputStream(Uri
								.parse(originalUri.toString())));
						if(bitMap!=null)bitMap.recycle();
						bitMap = getPicFromBytes(mContent, null);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				
				//在这边进行图片的旋转
				Bitmap resizedBitmap = null;
				if(Build.MODEL.equals("7260")){
					Matrix matrix = new Matrix();
					matrix.postRotate(270);
					resizedBitmap = Bitmap.createBitmap(bitMap, 0, 0,bitMap.getWidth(), bitMap.getHeight(), matrix, true);
					bitMap = resizedBitmap;
				}
					System.out
							.println("camera------------------585----------bitmap---"
									+ bitMap);
					// map = scaleImg(bitMap, 250, 420);
					System.out.println("Camera--------------75------");
					
					cam_photo.setImageBitmap(bitMap);
					ByteArrayOutputStream out = new ByteArrayOutputStream();
					// 对图片上传时候图片大小的选择
					if(set_map2!=null)set_map2.recycle();
					set_map2 = scaleImg(bitMap, x[position_s],
							y[position_s]);
					if(set_map2==null)return;
					// 将其写入输出流中
					set_map2.compress(Bitmap.CompressFormat.JPEG, 25, out);
					photo = out.toByteArray();
					try {
						out.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					intTobyte(photo.length); // 图片的长度
					System.out.println("camera-----------------------722-------"+photo_len);
					camera_paht = PHOTO_DIR + File.separator
							+ getPhotoFileName();
					System.out
							.println("camera ---------照片存储地址----------419-----"
									+ camera_paht);
					File aFile = new File(camera_paht); // 图片上传地址
					try {
						FileOutputStream fos = new FileOutputStream(aFile); // 文件输出流
						try {
							fos.write(photo);
							
							fos.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				//System.out.println("-----photo----" + photo.length);
				// Toast.makeText(Camera.this, "" + photo.length, 2).show();
				
			}
			break;
		}
			// 相册
		case IMAGE_CODE: {
			// camera_paht =PHOTO_DIR+File.separator+getPhotoFileName() ;
			Bundle bundle = data.getExtras();
			System.out.println("Camera--------------75------");
			paizhao = false;
			xiangce = true;
			Uri originalUri = data.getData();
			if (originalUri != null) {
				Bitmap bitMap = null;
				try {
					if(bitMap!=null)bitMap.recycle();
					bitMap = null;
					bitMap = MediaStore.Images.Media.getBitmap(resolver,
							originalUri);
					String[] proj = { MediaColumns.DATA };
					Cursor cursor = managedQuery(originalUri, proj, null, null,
							null);
					// 按我个人理解 这个是获得用户选择的图片的索引值
					int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
					System.out.println("706-------------camera------"
							+ column_index);
					// 将光标移至开头 ,这个很重要,不小心很容易引起越界
					cursor.moveToFirst();

					// 最后根据索引值获取图片路径
					ByteArrayOutputStream out = new ByteArrayOutputStream();
					uploadPhotPath = cursor.getString(column_index);
					System.out
							.println("camera--------------------561----------- 相册图片的地址--------"
									+ uploadPhotPath);
					Bitmap map = scaleImg(bitMap, 250, 420);
					map.compress(Bitmap.CompressFormat.JPEG, 25, out);
					photo = out.toByteArray();
					intTobyte(photo.length); // 图片的长度
					System.out
							.println("camera-----------------565----------相册图片的长度-----"
									+ photo.length);
					cam_photo.setImageBitmap(map);				
				} catch (Exception e) {
				    Toast.makeText(Camera.this, "图片不可用", 3).show();
				} // 获取相机返回的数据

			}

			break;
		}

		}
	}

图片的上传,以前是用特定的协议进行上传,不知道是协议的什么地方出现了,一直上传不成功。在网上找到另一种上传方法,Http有自己封装好的方法,

private void uploadFile2Svr2() {
		if (paizhao) {
			file = new File(camera_paht);
			// paizao图片的地址
		}
		if (xiangce) {
			if(uploadPhotPath==null)	
				return;
			file = new File(uploadPhotPath);
		}
			
			System.out.println("camera------------------------807");
			// 相册图片的地址

		String httpUrl = path;
		System.out.println("---------------");
		HttpPost request = new HttpPost(httpUrl);
		HttpClient httpClient = new DefaultHttpClient();
		FileEntity entity = new FileEntity(file, "binary/octet-stream");
		HttpResponse response;
		request.setEntity(entity);
		entity.setContentEncoding("binary/octet-stream");
		try {
			response = httpClient.execute(request);
			System.out.println(response.toString());
		} catch (ClientProtocolException e) {
			Toast.makeText(Camera.this, "图片不可用,请重新拍摄上传", 3).show();
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Toast.makeText(Camera.this, "图片不可用,请重新拍摄上传", 3).show();
			e.printStackTrace();
		}
		}

可能有些混乱,可以从当中抽取自己需要的部分,不是所有的都适合自己

展开阅读全文
打赏
2
25 收藏
分享
加载中
有没有源码啊 714088665@qq.com
2015/05/08 11:29
回复
举报
你好,可以把源码发送到我1179421200@qq.com里吗?
2014/01/04 10:49
回复
举报
能不能给一份源码
804672870@qq.com
多谢~
2013/04/26 16:32
回复
举报
如果在上传图片的时候,Sd卡突然拔出来了,怎么办?
2013/02/21 16:01
回复
举报
如果在上传图片的时候,Sd卡突然拔出来了,怎么办?
2013/02/21 16:01
回复
举报
能发个工程文件给我? 谢谢~346631756@qq.com
2013/02/16 16:07
回复
举报
有完整的代码么 能不能发个到asdf245245@163.com 谢谢
2012/12/29 15:17
回复
举报
更多评论
打赏
7 评论
25 收藏
2
分享
返回顶部
顶部