当从图片库图片被选中的应用程序崩溃 [英] App crashes when image from gallery gets selected

查看:244
本文介绍了当从图片库图片被选中的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序的摄像头功能真正推动我坚果!

The camera function in my app really driving me in nuts !!!

我建立我的应用程序的摄像头功能,但它坠毁时,我试图从图库中选择图片。

I have built an camera function in my app, but it crashed when I try to select the image from gallery.

Bitmap photo;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
ImageView imageView;

  private void activeTakePhoto() {  // if select open camera 
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

    private void activeGallery() { // if select choose from gallery
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, RESULT_LOAD_IMAGE);
    }

 @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                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);
                    cursor.close();
                    Bitmap a = (BitmapFactory.decodeFile(picturePath));
                    photo = scaleBitmap(a, 200, 200);
                    imageView.setImageBitmap(photo);
                    //photo = decodeSampledBitmapFromUri(picturePath, 100, 20);
                    imageView.setImageBitmap(photo);
                }

            case REQUEST_IMAGE_CAPTURE:
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

                //to generate random file name
                String fileName = "tempimg.jpg";

                try {
                    photo = (Bitmap) data.getExtras().get("data");
                    //captured image set in imageview
                    imageView.setImageBitmap(photo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
    }

如果我删除 activeTakePhoto()情况下REQUEST_IMAGE_CAPTURE毕竟该行:,选定的图像可以在的ImageView 显示。

If I remove the activeTakePhoto() and all the line after case REQUEST_IMAGE_CAPTURE:, the selected image can be displayed on imageView.

如果我删除 activeGallery(),拍摄的图像可以在的ImageView

If I remove activeGallery(), the captured image can be display on imageView.

但是,如果我用的片段code我张贴在这里(这两个功能),它坠毁时,我从库中选择一个图像的的logcat 错误

But, if I use the snippet code I posted here (both function), it crashed when I select an image from gallery with the logCat error

 12-10 14:49:03.507      927-927/? E/HwSystemManager﹕ :ACTION_BATTERY_CHANGED pluged =2
    12-10 14:49:07.020  19428-19428/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/832821 flg=0x1 }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.AddMoreClaims}: java.lang.NullPointerException
                at android.app.ActivityThread.deliverResults(ActivityThread.java:3579)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3622)
            at android.app.ActivityThread.access$1100(ActivityThread.java:169)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1436)

和线路位图的缩略图=(位图)data.getExtras()获得(数据); 。然而,当我选择捕获映像,拍摄的图像仍然可以显示在的ImageView

and the line is Bitmap thumbnail = (Bitmap) data.getExtras().get("data");. However, when I select capture image, the captured image still can display on imageView.

推荐答案

你缺少休息;之间切换的情况下阻止。结果
一旦突破放,你会不会打通异常作为秋季不会发生在案REQUEST_IMAGE_CAPTURE

you are missing break; between switch cases block.
Once break is put, you will not get exception as fall through will not happen to case 'REQUEST_IMAGE_CAPTURE'

这篇关于当从图片库图片被选中的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆