zoukankan      html  css  js  c++  java
  • android 照相相册

    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/*";
    private static Uri photoUri;
    /**
    * 进入照相机
    * @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);
    }

    /**
    * 相册||照相 结果返回处理
    * @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) {
    AppManager applicationContext = (AppManager)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;

    }

  • 相关阅读:
    Nginx负载均衡+代理+ssl+压力测试
    Nginx配置文件详解
    HDU ACM 1690 Bus System (SPFA)
    HDU ACM 1224 Free DIY Tour (SPFA)
    HDU ACM 1869 六度分离(Floyd)
    HDU ACM 2066 一个人的旅行
    HDU ACM 3790 最短路径问题
    HDU ACM 1879 继续畅通工程
    HDU ACM 1856 More is better(并查集)
    HDU ACM 1325 / POJ 1308 Is It A Tree?
  • 原文地址:https://www.cnblogs.com/gfqFighting/p/3223477.html
Copyright © 2011-2022 走看看