Android拍照或者从手机相册里选择图片

				AlertDialog.Builder builder = new Builder(DiaryActivity.this);
				builder.setItems(new String[] { "拍照", "从手机相册里选择" }, new OnClickListener()
				{

					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						if (which == 0) // 拍照
						{
							Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
							startActivityForResult(camera, ImageUtils.IMAGE_CAMERA);
						}
						else if (which == 1) // 从手机相册选择
						{
							Intent picture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
							startActivityForResult(picture, ImageUtils.IMAGE_PHONE);
						}
					}
				});
				Dialog dialog = builder.create();
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.show();


/**
	 * 获取相机拍照的图片
	 * @param data
	 * @return
	 */
	private String getCameraImage(Bundle bundle)
	{
		String strState = Environment.getExternalStorageState();
		if (!strState.equals(Environment.MEDIA_MOUNTED))
		{
			Log.i("Test File", "SD card is not available/writealble right now!");
		}
		String fileName = new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
		// Bundle bundle = data.getExtras();
		Bitmap bitmap = (Bitmap) bundle.get("data");
		File file = new File("/sdcard/com.test.diary/");
		if (!file.exists())
		{
			file.mkdirs();
		}
		fileName = "/sdcard/com.test.diary/" + fileName;
		FileOutputStream stream = null;
		try
		{
			stream = new FileOutputStream(fileName);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (stream != null)
				{
					stream.flush();
					stream.close();
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		// ((ImageView) findViewById(R.id.iv_temp)).setImageBitmap(bitmap);
		return fileName;
	}
	
	
	
		/**
	 * 获取系统缺省路径选择的图片
	 * @param data
	 * @return
	 */
	private String getPhoneImage(String uriString)
	{

		// Uri selectedImage = data.getData();
		Uri selectedImage = Uri.parse(uriString);
		String[] filePathColumns = { MediaStore.Images.Media.DATA };
		Cursor cursor = this.getContentResolver().query(selectedImage, filePathColumns, null, null, null);
		cursor.moveToFirst();
		int columnIndex = cursor.getColumnIndex(filePathColumns[0]);
		String fileName = cursor.getString(columnIndex);
		cursor.close();

		// ((ImageView) findViewById(R.id.iv_temp)).setImageBitmap(BitmapFactory.decodeFile(picturePath));
		return fileName;
	}












你可能感兴趣的:(Android拍照或者从手机相册里选择图片)