Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何显示用户库中大小合适的图像?_Android_Android Intent_Android Bitmap_Android Gallery_Bitmapfactory - Fatal编程技术网

Android 如何显示用户库中大小合适的图像?

Android 如何显示用户库中大小合适的图像?,android,android-intent,android-bitmap,android-gallery,bitmapfactory,Android,Android Intent,Android Bitmap,Android Gallery,Bitmapfactory,在我的两项活动中,我都在努力展示合适大小的图像。 CatalogActivity中的图像看起来像是经过适当的缩放和裁剪,而EditorActivity中的图像是完整的,并且更小。 唯一的区别是目录中的图像来自图像资源,而编辑器图像来自库,所以来自Uri 你能告诉我为什么会有这种差异,以及如何使EditorActivity中的图像与另一个图像相同吗 从目录活动: // Make the sample image smaller so it doesn't take too much space i

在我的两项活动中,我都在努力展示合适大小的图像。 CatalogActivity中的图像看起来像是经过适当的缩放和裁剪,而EditorActivity中的图像是完整的,并且更小。 唯一的区别是目录中的图像来自图像资源,而编辑器图像来自库,所以来自Uri

你能告诉我为什么会有这种差异,以及如何使EditorActivity中的图像与另一个图像相同吗

从目录活动:

// Make the sample image smaller so it doesn't take too much space in the     memory
Bitmap sourdoughBitmap = ProductsEntry.decodeSampledBitmapFromResource(getResources(), R.drawable.sourdough_picture, 72, 72);
从EditorActivity:

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

        if (requestCode == RESULT_LOAD_PICTURE && resultCode == RESULT_OK && null != intent) {
            Uri selectedPictureUri = intent.getData();
            // Show the selected picture in an ImageView in the editor
            try {
                // Scale the bitmap received from the uri so it fits in the small ImageView
                scaledPictureBitmap = ProductsEntry.decodeSampledBitmapFromUri(this, selectedPictureUri, 72, 72);
                // Hide the gray picture placeholder
                mImageView.setBackgroundResource(0);
                // Show the scaled bitmap in the ImageView
                mImageView.setImageBitmap(scaledPictureBitmap);
                // The user has chosen a picture and we can change
                // the text of the button to say "Change picture"
                mAddPictureButton.setText(R.string.edit_product_change_photo);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
来自ProductsEntry:

/**
         * Helps create a smaller bitmap image from resource
         * @param resources - a resources object
         * @param resourceId - the id of the image in the drawable folder
         * @param requiredWidth - the width that we want for the final image
         * @param requiredHeight - the height that we want for the final image
         * @return the decoded Bitmap
         */
        public static Bitmap decodeSampledBitmapFromResource(Resources resources, int resourceId,
                                                             int requiredWidth, int requiredHeight){
            // First decode with inJustDecodeBounds = true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(resources, resourceId, options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);

            // Decode bitmap with inJustDecodeBounds = false
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeResource(resources, resourceId, options);
        }

        /**
         * Helps create a smaller bitmap image from a uri
         * @param context - a context object
         * @param uri - the uri of the image
         * @param requiredWidth - the width that we want for the final image
         * @param requiredHeight - the height that we want for the final image
         * @return the decoded Bitmap
         */
        public static Bitmap decodeSampledBitmapFromUri(Context context, Uri uri, int requiredWidth, int requiredHeight)
        throws FileNotFoundException {
            // First decode with inJustDecodeBounds = true, only to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);

            // Decode bitmap with inJustDecodeBounds = false
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
        }

        /**
         * Calculate a sample size value that is a power of 2 based on a target width and height
         * @param options is used to pass options to the BitmapFactory
         * @param requiredWidth is the width that we want for the final image
         * @param requiredHeight is the height that we want for the final image
         * @return by how much to scale down the image
         */
        private static int calculateInSampleSize(BitmapFactory.Options options, int requiredWidth, int requiredHeight) {
            // Raw height and width of the image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > requiredHeight || width > requiredWidth) {

                final int halfHeight = height / 2;
                final int halfWidth = width / 2;

                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the required height and width
                while ((halfHeight / inSampleSize) >= requiredHeight
                        && (halfWidth / inSampleSize) >= requiredWidth){
                    inSampleSize *= 2;
                }
            }

            return inSampleSize;
        }
从activity_editor.xml:

<ImageButton
     android:id="@+id/picture"
     android:layout_height="72dp"
     android:layout_width="72dp"
     android:src="@drawable/ic_photo"
     android:layout_gravity="center" />


链接到存储库。

请将所有位图分辨率(WxH)放入代码中,以便我们进行比较。同样适用于样本大小和要求的大小。在底部的问题中添加缺少的信息。样本量是用上一种方法计算的,要求的高度是72,宽度是72 dp。你没有给出我要求的信息。我想我不太明白你要求的是什么。你能解释一下吗?链接到问题下方的存储库,这足够了吗?