调用系统相机拍照获取图片或者从相册中调用本地图片显示问题

            需求是这样的:打开系统或者第三方相机app拍照,然后使用所拍照片进行处理,或者直接调用系统或者第三方相册app,选择图片返回使用。需求很简单,过程很蛋疼,网上有很多方法,不过有很多不好用的地方,最后使用的这种方式还不错,记录一下,还是那句话,我很懒,以后复制使用直接复制代码:

           1.调用相机拍照返回所拍照片(不是缩略图),点击按钮,调用相机:

          Intent intent = new Intent();
          intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
          File file = new File(getExternalFilesDir(Environment.DIRECTORY_DCIM) +"/bidata");
          if (!file.exists()) {
                 file.mkdirs();
             }
          intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
          intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(file, "cameraTmp.png")));
          startActivityForResult(intent, 0);
    获取所得照片:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 0:
                     Bitmap bitmap = CommonUtils.getBitmap(this, getExternalFilesDir(Environment.DIRECTORY_DCIM) + "/bidata/"+ "cameraTmp.png");
                    break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
2.调用相册获取图片

     Intent picture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     startActivityForResult(picture, 1);
        获取所得到的图片:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1:
                    CommonUtils.bitmap = BitmapFactory.decodeFile(CommonUtils.getRealFilePath(this, data.getData()));
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
  最后把两个加载bitmap的方法贴出来:

    /**
     * 由string类型的路径加载图片
     *
     * @param context
     * @param path
     * @return
     */
    public static Bitmap getBitmap(Context context, String path) {

        Uri uri = Uri.fromFile(new File(path));
        InputStream in = null;
        try {
            in = context.getContentResolver().openInputStream(uri);
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            BitmapFactory.decodeStream(in, null, o);
            in.close();

            int scale = 1;
            if (o.outHeight > 1024 || o.outWidth > 1024) {
                scale = (int) Math.pow(
                        2,
                        (int) Math.round(Math.log(1024 / (double) Math.max(
                                o.outHeight, o.outWidth)) / Math.log(0.5)));
            }
            if (scale < 4) {
                scale = 4;
            }
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            in = context.getContentResolver().openInputStream(uri);
            Bitmap b = BitmapFactory.decodeStream(in, null, o2);
            in.close();

            return b;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
        return null;
    }
    /**
     * 将图片的uri转为绝对路径
     *
     * @param context
     * @param uri
     * @return
     */
    public static String getRealFilePath(final Context context, final Uri uri) {
        if (null == uri)
            return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri,
                    new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }
       也 希望对有需要的战友有帮助。。。。



阅读更多

没有更多推荐了,返回首页