【问题标题】:How I can resize images taken from the Gallery?如何调整从图库中拍摄的图像的大小?
【发布时间】:2014-05-10 12:14:46
【问题描述】:

我正在尝试:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, RESULTADO_GALERIA);
.
.
.

String imgPath = data.getDataString(); // return something like "/external/images/media/13852"
BitmapFactory.decodeFile(imgPath);  // allways null...

我也试过了:

File bitmapFile = new File(Environment.getExternalStorageDirectory()
                + "/" + imgPath);
String aux = bitmapFile.getAbsolutePath();  // /storage/sdcard0/external/images/media/13852
BitmapFactory.decodeFile(bitmapFile.getAbsolutePath()); // NULL

结果是一样的

对我来说唯一的工作就是手动放置路径...

File file = new File("/storage/sdcard0/DCIM/Camera/"+"IMG_20140506_101341.jpg"); // nothing to do with the path obtained by code...
BitmapFactory.decodeFile(bitmapFile.getAbsolutePath());  // OK

如何获得画廊图像的真实路径?调整它们大小的其他想法?

谢谢!

【问题讨论】:

    标签: android bitmapfactory


    【解决方案1】:

    我已经证明了这两种情况

    1) 从相机拍摄照片

    2) 从画廊拍照

    试试这对我很有魅力

    private String selectedImagePath = "";
        final private int PICK_IMAGE = 1;
        final private int CAPTURE_IMAGE = 2;
    
    public Uri setImageUri() {
            // Store image in dcim
            File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
            Uri imgUri = Uri.fromFile(file);
            this.imgPath = file.getAbsolutePath();
            return imgUri;
        }
    
    
        public String getImagePath() {
            return imgPath;
        }
    
    btnGallery.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
    
                }
            });
    
            btnCapture.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                    startActivityForResult(intent, CAPTURE_IMAGE);
                }
            });
    
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode != Activity.RESULT_CANCELED) {
                if (requestCode == PICK_IMAGE) {
                    selectedImagePath = getAbsolutePath(data.getData());
                    imgUser.setImageBitmap(decodeFile(selectedImagePath));
                } else if (requestCode == CAPTURE_IMAGE) {
                    selectedImagePath = getImagePath();
                    imgUser.setImageBitmap(decodeFile(selectedImagePath));
                } else {
                    super.onActivityResult(requestCode, resultCode, data);
                }
            }
    
        }
    
    
    public Bitmap decodeFile(String path) {
            try {
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(path, o);
                // The new size we want to scale to
                final int REQUIRED_SIZE = 70;
    
                // Find the correct scale value. It should be the power of 2.
                int scale = 1;
                while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                    scale *= 2;
    
                // Decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeFile(path, o2);
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return null;
    
        }
    
    public String getAbsolutePath(Uri uri) {
            String[] projection = { MediaColumns.DATA };
            @SuppressWarnings("deprecation")
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }
    

    【讨论】:

    • 谢谢。完美运行。问候!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    相关资源
    最近更新 更多