• android 调用相册-照相机封装


    把这两个功能封装起来,经常使用到。

    private static Uri photoUri;
    	public static final int NONE = 0;
    	public static final int PHOTOHRAPH = 1;// 拍照
    	public static final int PHOTOZOOM = 2; // 缩放
    	public static final int PHOTORESULT = 3;// 结果
    	public static final String IMAGE_UNSPECIFIED = "image/*";
    	/**
    	 * 进入照相机
    	 * @param activity
    	 */
    	public static void startCamera(Activity activity) {
    		  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE"  
              ContentValues values = new ContentValues();    
              photoUri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);    
              intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);  
              /**-----------------*/  
              activity.startActivityForResult(intent, PHOTOHRAPH);  
    	}
    	
    	/**
    	 * 进入相册
    	 * @param activity
    	 */
    	public static  void startPhotoAlbum(Activity activity) {
    		Intent intent = new Intent(Intent.ACTION_PICK, null);  
            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);  
            activity.startActivityForResult(intent, PHOTOZOOM);
    	}
    	
    	/**
    	 * 裁剪照片 跳系统的activity
    	 * @param activity
    	 * @param uri
    	 */
    	public static void startPhotoZoom(Activity activity, Uri uri) {
    		Intent intent = new Intent("com.android.camera.action.CROP");
    		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
    		intent.putExtra("crop", "true");
    		// aspectX aspectY 是宽高的比例
    		intent.putExtra("aspectX", 1);
    		intent.putExtra("aspectY", 1);
    		// outputX outputY 是裁剪图片宽高
    		intent.putExtra("outputX", 300); // 决定图片的像素
    		intent.putExtra("outputY", 300); // 决定图片的像素
    		intent.putExtra("return-data", true);
    		activity.startActivityForResult(intent, PHOTORESULT);
    	}
    	
    
            
    	/**
    	 * 相册||照相 结果返回处理 放在Activity的onActivityResult里
    	 * @param activity
    	 * @param requestCode
    	 * @param resultCode
    	 * @param data
    	 * @return
    	 */
    	public static Object[] handleResultFromCameraOrPhotos(Activity activity ,int requestCode, int resultCode, Intent data) {
    		 if (resultCode == NONE)  
    	            return null;  
    	        // 拍照  
    	        if (requestCode == PHOTOHRAPH) {  
    	        	AppContext applicationContext = (AppContext)activity.getApplicationContext();
    				String name="temp";//+applicationContext.setNum();
    				name="temp";
    	        	String path = Environment.getExternalStorageDirectory()+"/"+name+".jpg";
    	        	System.out.println(path);
    	            File picture = new File(path);
    	            photoUri = data.getData();
    	            startPhotoZoom(activity,photoUri);  
    	        }  
    	          
    	        if (data == null)  
    	            return null;  
    	          
    	        // 读取相册缩放图片  
    	        if (requestCode == PHOTOZOOM) { 
    	        	photoUri = data.getData();
    	            startPhotoZoom(activity,data.getData());  
    	        }  
    	        // 处理结果  
    	        if (requestCode == PHOTORESULT) {  
    	            Bundle extras = data.getExtras();  
    	            if (extras != null) {  
    	            	//photoUri = data.getData();
    	            	String picPath = null;
    	            	  String[] pojo = {MediaStore.Images.Media.DATA};  
    	                  Cursor cursor = activity.managedQuery(photoUri, pojo, null, null,null);     
    	                  if(cursor != null )  
    	                  {  
    	                	  
    	                      int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);  
    	                      cursor.moveToFirst();  
    	                      picPath = cursor.getString(columnIndex);  
    	                      //cursor.close();  
    	                  }  
    	            	
    					Log.d("TAG", picPath +"");
    	            	Bitmap photo = extras.getParcelable("data");  
    	                ByteArrayOutputStream stream = new ByteArrayOutputStream();  
    	                photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);// (0 - 100)压缩文件  
    	                Object[] hashMap = new Object[2];
    	                hashMap[1] = photo;
    	                hashMap[0] = picPath;
    	                //photo.recycle();
    	                return hashMap; 
    	            }
    	        }
    			return null;
    		
    	}    
    

      

  • 相关阅读:
    sql 同一行中,不同结果在不同列显示
    用jstl的if或when标签判断字符串是否为空
    RESTful 接口规范
    HTTP协议详解
    easyui datagrid 去除单击行选中事件
    easyui datagrid 选中行效果
    Nodejs
    VMware 中的win7虚拟机在一段时间后就会自动挂起
    Vmware 安装 ghost 版 win 7
    idea tomcat 乱码问题的解决及相关设置
  • 原文地址:https://www.cnblogs.com/gfqFighting/p/3900703.html
Copyright © 2020-2023  润新知