如何从库中选择图像,重新调整大小并将其重新保存到其他图库文件夹

时间:2016-03-10 18:27:25

标签: android

我在SO上发现了一些代码,帮助我调出一个图像选择器,保存图像,重新调整大小/解码并在ImageView中显示它,但是我想知道如何将这个新重新调整大小的图像保存到Gallery中的另一个文件夹,然后将其新位置(包括文件名)作为String返回。以下是我到目前为止的情况:

启动图像选择器意图:

public void openGallery() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);
    }

获取所选图像:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {
            case SELECT_PHOTO:
                if (resultCode == RESULT_OK) {

                    try {
                        Uri selectedImage = imageReturnedIntent.getData();
                        InputStream imageStream = getContentResolver().openInputStream(selectedImage);
                        Bitmap yourSelectedImage = decodeUri(selectedImage);
                        imageIcon.setImageBitmap(yourSelectedImage);
                        imageText.setText("");
                    } catch (Exception e) {

                        Toast.makeText(this, "Image Selection Error", Toast.LENGTH_LONG).show();
                    }
                }
        }
    }

解码图像的功能:

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 200;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);

    }

我也不确定我是否正确实现了这个解码功能,因为更改REQUIRED_SIZE int在ImageView中显示时没有任何区别。

1 个答案:

答案 0 :(得分:0)

我设法用以下方法解决了这个问题:

public void saveResizedImage(Bitmap image) {

        File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "rapical" + File.separator);
        String path = root.getPath();
        OutputStream fOut = null;
        File noOfImages[] = root.listFiles();
        int fileCount = 0;
        for (int i = 0; i < noOfImages.length; i++) {
            fileCount++;
        }
        File foodImageFile = new File(path, "rapical" + fileCount + ".png");
        try {
            fOut = new FileOutputStream(foodImageFile);
            image.compress(Bitmap.CompressFormat.PNG, 50, fOut);
            fOut.flush();
            fOut.close();
            String imagePath = path + "/rapical" + fileCount + ".png";
            Log.w("ImagePath: ", imagePath);

        } catch (Exception e) {

        }
    }