-5

我试图从我的 android 应用程序在 Facebook 墙上发布图像。为此,我从布局创建了一个位图。这是我的代码

                    System.gc();
            Bitmap bitmap = Bitmap.createBitmap(
                    view.getMeasuredWidth(),
                    view.getMeasuredHeight(),
                    Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Drawable bgDrawable = newLinearLayout.getBackground();
            if (bgDrawable != null)
                bgDrawable.draw(canvas);
            else
                canvas.drawColor(Color.WHITE);
            view.draw(canvas);
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(1.0f, 1.0f);

            // recreate the new Bitmap
            Bitmap resizedBitmap = null;
            resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            //Canvas resizedCanvas = new Canvas(resizedBitmap);

            if (bitmap != null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                Bundle parameters = new Bundle();
                byte[] data = baos.toByteArray();
                parameters.putByteArray("picture", data);
                DataKeeper.getInstance().setBundleToPost(parameters);

            }
            bitmap.recycle();

它显示OutOfMemory 异常。我知道这是由于图像大小超出了其可用大小。如何在不损失质量的情况下调整图像大小?我尝试使用BitmapFactory.Options更正此问题。但不起作用。如何解决这个异常?

问候阿莎

4

3 回答 3

0

用这个 :

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

有关更多详细信息,请参阅此链接

关联

于 2012-08-13T10:44:49.823 回答
0

请查看本教程并下载示例项目。这将解决您的问题

http://developer.sonymobile.com/wp/2011/06/27/how-to-scale-images-for-your-android%E2%84%A2-application/

于 2012-08-13T11:57:02.280 回答
0

@Zaz Gmy 的想法是正确的,
但是我不认为inSampleSize每个图像的值都应该是恒定的(8)。

这当然会起作用,但是在相同的比例因子 = 8 上缩放具有不同分辨率的不同图像会导致一些图像的质量明显下降。

相反,inSimpleSize应该根据目标widthheight图像生成。

Android 文档提供了一篇关于如何处理此问题的好文章。

于 2012-08-13T10:53:06.093 回答