从摄像头拍摄图像到ImageView的问题

时间:2013-07-25 15:11:14

标签: android bitmap imageview

我想让用户设置某个联系人图标,用户可以拍照或从图库中选择一个。我有以下开始活动的结果,注意我在这里处理两个不同,但他们应该处理相同:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case Constants.TAKE_PHOTO:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                File file = new File(selectedImage.getPath());
                mPhoto = decodeFile(file);
                Bitmap croppedImage = cropImage(mPhoto);
                Bitmap resized = Bitmap.createScaledBitmap(croppedImage, 100, 100, true);
                Bitmap finalPhoto = getRoundedRectBitmap(resized, 100);
                imageView.setImageBitmap(finalPhoto);
            }
            break;

        case Constants.CHOOSE_PHOTO:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                imageView.setImageURI(selectedImage);
            }
            break;
    }

第二种情况,CHOOSE_PHOTO有效。我从图库中选择了一张照片,它放在了ImageView中。问题是,我在第一种情况下创建的方法TAKE_PHOTO很重要,Bitmap被重新整形成一个圆圈并相应地裁剪;对于小尺寸的图片,这完全符合我的要求。主要错误是Out of Memory。我在SO上查看了它,并找到了以下方法来处理这个问题:

private Bitmap decodeFile(File f) {
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

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

        //Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

我认为这会占用Bitmap尺寸,在将Bitmap加载到内存之前记录内容,然后在加载之前缩放Bitmap,直到完全避免OOM错误。但是,当我运行代码时,应用程序仍然在尝试确认拍摄照片以供选择时崩溃,就像确实发生了OOM错误一样。在崩溃时我有以下Logcat:

07-25 11:01:51.396  13054-13054/com.example.android.home E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.android.home/com.tabletnanny.ChildSelection}: java.lang.NullPointerException
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3322)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3365)
    at android.app.ActivityThread.access$1200(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1315)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5059)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.tabletnanny.ChildSelection.onActivityResult(ChildSelection.java:126)
    at android.app.Activity.dispatchActivityResult(Activity.java:5242)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3318)

我在几个小时的大部分时间里一直在研究这个问题,但似乎无法弄清楚如何有效地加载Bitmap。我查看了Android Dev网站,他们在这里有一个关于处理大型Bitmaps的有用教程,但他们的解决方案与我在decodeFile方法中在SO上找到的解决方案非常相似。我在这里缺少什么?

1 个答案:

答案 0 :(得分:3)

您可以尝试在ONActivityResult()

中获取位图
    Bitmap picture = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    picture.compress(Bitmap.CompressFormat.JPEG, 100, stream);