从Camera Photo设置ImageButton时的内存/分配错误

时间:2014-01-03 10:34:45

标签: android image android-camera

我正在尝试从手机摄像头拍摄照片并将其放在ImageButton中作为个人资料活动的一部分,包括所有用户详细信息,然后将图像另存为共享。

如果我使用以下代码,则ImageButton不会更新:

    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
            // method checks data returned form camera via startActivityForResult
            super.onActivityResult(requestCode, resultCode, data);

                Runnable runnable = new Runnable(){@Override
                    public void run() {
                        handler.post(new Runnable(){

                            @Override
                            public void run() {
                                Bundle extras = data.getExtras();
                                                            photo = (Bitmap) extras.get("data");

                                                takeAndSetPhoto.setImageBitmap(photo);
                                Toast.makeText(getBaseContext(), "Image set to profile!",
                                        Toast.LENGTH_SHORT).show();
                            }//edn inner run


                        });//end new runnab;e

                    }

                };
                new Thread(runnable).start();

            }//end if result OK     
        }// end onActivity

或者,如果我使用这种方法,图像会加载,但我得到了错误:

  

缩放位图的分配失败   Out Of Memory 01-03 10:13:06.645:E / AndroidRuntime(30163):   android.view.InflateException:二进制XML文件行#18:错误   膨胀班

代码使用Uro拍摄照片:

                    public void run() {
                        Bundle extras = data.getExtras();
                        Uri photoShot = data.getData(); view
                        takeAndSetPhoto.setImageURI(photoShot);
                        Toast.makeText(getBaseContext(), "Image set to profile!",
                                Toast.LENGTH_SHORT).show();
                    }//edn inner run

所有建议都表示赞赏。 干杯 夏兰

调整图片大小: 调整图像大小时:

Bitmap photoBitmap = Bitmap.createScaledBitmap(photo, 100, 100, false);

在SD卡激活的模拟器上工作正常,但在真实设备(8英寸平板电脑)上崩溃

1 个答案:

答案 0 :(得分:0)

问题是我从设备手机拍摄时没有调整图像大小,在设置图像视图之前将图像传递给此方法:

public Bitmap reSizeImage(Bitmap bitmapImage) {
        // resize bitmap image passed and rerun new one
        Bitmap resizedImage = null;
        float factorH = h / (float) bitmapImage.getHeight();
        float factorW = w / (float) bitmapImage.getWidth();
        float factorToUse = (factorH > factorW) ? factorW : factorH;
        try {
            resizedImage = Bitmap.createScaledBitmap(bitmapImage,
                    (int) (bitmapImage.getWidth() * factorToUse),
                    (int) (bitmapImage.getHeight() * factorToUse), false);
        } catch (IllegalArgumentException e) {
            Log.d(TAG, "Problem resizing Image @Line 510+");
            e.printStackTrace();
        }
        Log.d(TAG,
                "in resixed, value of resized image: "
                        + resizedImage.toString());
        return resizedImage;
    }// end reSize