【问题标题】:Getting resource ID from of image from gallery从图库中获取图像的资源 ID
【发布时间】:2014-05-06 06:40:05
【问题描述】:

在尝试从 BitmapFactory 找到解决内存不足错误的解决方案后,我发现了这个 tutorial。 我明白除了这条线之外的所有内容 mImageView.setImageBitmap( decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

第二个参数询问图像的资源 ID,我不确定它来自哪里。这是我的代码的样子:

    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadImage);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }


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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);


            //http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(getResources(), R.id.imgView, options);
            int imageHeight = options.outHeight;
            int imageWidth = options.outWidth;
            String imageType = options.outMimeType;

            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            //imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            imageView.setImageBitmap(
                    decodeSampledBitmapFromResource(getResources(),res, imageView.getWidth(), imageView.getHeight()));
            cursor.close();
        }

    }
    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        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 requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}
        public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

【问题讨论】:

    标签: java android bitmap bitmapfactory


    【解决方案1】:

    资源 ID 引用了应用程序的 res/drawable 文件夹中的位图。如果图像在其他地方(例如画廊),您必须找到其他方法来加载图像并获取位图。看看BitmapFactory 提供的方法。

    【讨论】:

    • 我原来是用imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));它适用于较小的图像。对于任何大图像,我都遇到了内存不足的异常。有没有办法把picturePath转成字符串?
    • 你为什么要把路径改成一个sting?无论如何,您必须阅读图片文件。尝试BitmapFactory.decodeFile(picturePath, options),对于选项,请使用您在问题的decodeSampledBitmapFromResource() 方法中的options.inSampleSize 位。这样一来,您将获得一个不会破坏您的记忆的较小图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2020-10-24
    相关资源
    最近更新 更多