Android 代码片段---从相册或相机获取图片保存并处理

private void startPhotoAlbum()
{
	Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		
	startActivityForResult(intent, UConstants.GETIMAGE);
}
	
private String urlTempPic = "";
	
private void startCamera()
{
	Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
	urlTempPic = getImagePath();
		
	i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(urlTempPic)));
	startActivityForResult(i, UConstants.CAPUTRE);
}
public static String getImagePath()
{
	String path = null;
			
	String dir = getExternalStoragePublicDirectory(UDataStorage.Dir_Pictures) + "/pyj/temp/pics/";
	path = dir + System.currentTimeMillis() + ".jpg";
			
	ensureDir(dir);
			
	return path;
}
public static boolean ensureDir(String dir)
{
	boolean result = false;
	File file = new File(dir);
	if (!file.exists())
		result = file.mkdirs();
	return result;
}
public static File getExternalStoragePublicDirectory(String type)
{
	File file = null;
	String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + type;
	file = new File(path);
	if (!file.exists())
		file.mkdirs();
	return file;
}

private static final int DIALOG_YES_NO_LONG_MESSAGE = 1;
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		switch (id) {
		case DIALOG_YES_NO_LONG_MESSAGE:
            return new AlertDialog.Builder(JSelectEventTypeActivity.this,AlertDialog.THEME_HOLO_LIGHT)
                .setTitle("提示")
                .setPositiveButton("从相册", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
    
                        /* User clicked OK so do some stuff */
                    	startPhotoAlbum();
                    }
                })
                .setNeutralButton("从相机", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {


                        /* User clicked Something so do some stuff */
                    	startCamera();
                    }
                })
                .setNegativeButton("不需要", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                })
                .create();
		}
		
		return null;
	}

@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (resultCode) {
		case RESULT_OK:
			if (requestCode == UConstants.CAPUTRE)
			{
				// Load up the image's dimensions not the image itself
				try {
					BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
					bmpFactoryOptions.inJustDecodeBounds = true;
					Bitmap bmp = BitmapFactory.decodeFile(urlTempPic,
							bmpFactoryOptions);
					
					int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) 1080);


					if (widthRatio > 1)
					{
						bmpFactoryOptions.inSampleSize = widthRatio;
					}


					bmpFactoryOptions.inJustDecodeBounds = false;
					bmp = BitmapFactory.decodeFile(urlTempPic, bmpFactoryOptions);

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			else if (requestCode == UConstants.GETIMAGE)
			{
				Uri imageFileUri = data.getData();
				
				try {
					BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
					bmpFactoryOptions.inJustDecodeBounds = true;
					
					Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
					
					int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
							/ (float) 1080);
					if (widthRatio > 1) {
						bmpFactoryOptions.inSampleSize = widthRatio;
					}
					
					bmpFactoryOptions.inJustDecodeBounds = false;
					bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri),null, bmpFactoryOptions);
		
					
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			break;


		default:
			break;
		}
	}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值