当前位置:  开发笔记 > 编程语言 > 正文

Android:使用Camera Intent时,应用程序在onActivityResult上崩溃

如何解决《Android:使用CameraIntent时,应用程序在onActivityResult上崩溃》经验,为你挑选了2个好方法。

我正在使用相机意图在我的应用程序中捕获图像.我的应用程序在Android 5.0.2使用相机时崩溃的问题.我正在使用片段的意图.下面是我片段内的代码:

拍照的方法

private void takePhoto() {
    mHighQualityImageUri = Util.generateTimeStampPhotoFileUri(getActivity());
    Log.d(UploadPicturesFragment.class.getSimpleName(),
            "URI: " + mHighQualityImageUri.toString());
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mHighQualityImageUri);
    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}

我片段中的onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
        return;
    }

    if (requestCode == REQUEST_IMAGE_CAPTURE) {
    Log.d(UploadPicturesFragment.class.getSimpleName(),
                "IMAGE URI NOT NULL: " + (mHighQualityImageUri == null));
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),
                    mHighQualityImageUri);
            DroomUtil.beginCrop(mHighQualityImageUri, getActivity(), this, true, bitmap.getWidth(),
                    bitmap.getHeight());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

记录跟踪

12-29 10:28:03.491: E/AndroidRuntime(9780): java.lang.RuntimeException:       Unable to resume activity {in.droom/in.droom.activity.MainActivity}:     java.lang.RuntimeException: Failure delivering result    ResultInfo{who=android:fragment:2, request=1, result=-1, data=null} to activity   {in.droom/in.droom.activity.MainActivity}: java.lang.NullPointerException:   Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()'   on a null object reference
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3224)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3257)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2479)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.os.Looper.loop(Looper.java:155)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.main(ActivityThread.java:5702)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at java.lang.reflect.Method.invoke(Native Method)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at java.lang.reflect.Method.invoke(Method.java:372)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
12-29 10:28:03.491: E/AndroidRuntime(9780): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:2, request=1, result=-1, data=null} to activity {in.droom/in.droom.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3881)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3197)
12-29 10:28:03.491: E/AndroidRuntime(9780):     ... 11 more
12-29 10:28:03.491: E/AndroidRuntime(9780): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.content.ContentResolver.openInputStream(ContentResolver.java:651)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:1019)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at in.droom.fragments.UploadPicturesFragment.onActivityResult(UploadPicturesFragment.java:395)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.Activity.dispatchActivityResult(Activity.java:6164)
12-29 10:28:03.491: E/AndroidRuntime(9780):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3877)
12-29 10:28:03.491: E/AndroidRuntime(9780):     ... 12 more

这条线没有.395是:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),
                    mHighQualityImageUri);

CommonsWare.. 12

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference

这意味着mHighQualityImageUrinull,在所有的可能性.如果你没有坚持Uri使用它,就会发生这种情况onSaveInstanceState().当您的应用程序处于后台并且相机应用程序位于前台时,您的进程完全可能会终​​止.



1> CommonsWare..:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference

这意味着mHighQualityImageUrinull,在所有的可能性.如果你没有坚持Uri使用它,就会发生这种情况onSaveInstanceState().当您的应用程序处于后台并且相机应用程序位于前台时,您的进程完全可能会终​​止.



2> Zubair Ahmed..:

按照以下步骤从相机拍摄照片并显示在上面 ImageView

1)启动相机意图

Uri fileUri;
String photoPath = "";

 private void startingCameraIntent()
    {
        String fileName = System.currentTimeMillis()+".jpg";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);
        fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, YOUR_REQ_CODE);
    }

2)回调onActivityResult函数

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode == Activity.RESULT_OK)
        {
            try
            {
                photoPath = getPath(fileUri);

                System.out.println("Image Path : " + photoPath);

                Bitmap b = decodeUri(fileUri);
                your_image_view.setImageBitmap(b);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }

3)decodeUri功能

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException
    {
        BitmapFactory.Options o = new BitmapFactory.Options();

        o.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(getContentResolver()
                                           .openInputStream(selectedImage), null, o);

        final int REQUIRED_SIZE = 72;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;

        int scale = 1;

        while (true)
        {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
            {
                break;
            }
            width_tmp /= 2;

            height_tmp /= 2;

            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();

        o2.inSampleSize = scale;

        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
                                                           .openInputStream(selectedImage), null, o2);

        return bitmap;
    }

4)图像的getPath

@SuppressWarnings("deprecation")
    private String getPath(Uri selectedImaeUri)
    {
        String[] projection = { MediaStore.Images.Media.DATA };

        Cursor cursor = managedQuery(selectedImaeUri, projection, null, null,
                                     null);

        if (cursor != null)
        {
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            return cursor.getString(columnIndex);
        }

        return selectedImaeUri.getPath();
    }

最后 在Manifest中定义权限

 

注意:如果您使用的是marshmallow(Android 6.0),则必须在使用相机应用程序之前设置权限检查.您可以在运行时阅读有关Android 请求权限的信息

推荐阅读
N个小灰流_701
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有