从另一个活动调用相机并获取父活动中的图像位置

时间:2016-06-30 09:44:12

标签: android android-intent android-camera-intent onactivityresult

我有一个类,其中包含调用相机意图的方法,然后保存图片并将图像位置发送到被调用的活动。但每当我点击相机图标时都会出现异常。

public String onCaptureImageResult() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createimage();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));


        }

    startActivityForResult(intent, TAKE_PICTURE);

}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE && data != null) {

        //String g = compressImage(mCurrentPhotoPath);
        String mCurrentPhotoPath = f.getAbsolutePath();
        g = compressImage(mCurrentPhotoPath);


    }

}

现在我需要父活动中的值g。我得到的例外如下。

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.snatarajan.checkcompress, PID: 26553
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
        at android.app.Activity.startActivityForResult(Activity.java:3745)
        at android.app.Activity.startActivityForResult(Activity.java:3706)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:861)
        at com.example.snatarajan.checkcompress.work.onCaptureImageResult(work.java:51)
        at com.example.snatarajan.checkcompress.MainActivity.onClick(MainActivity.java:254)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at                                                    
       06-30 15:16:47.447    844-31592/? E/DatabaseUtils﹕ Writing exception to parcel
       java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media/2 from pid=555, uid=1000 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
        at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:539)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:452)
        at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:443)
        at android.content.ContentProvider$Transport.openFile(ContentProvider.java:340)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:240)
        at android.os.Binder.execTransact(Binder.java:446)

从活动1开始我称之为

 w.onCaptureImageResult();

1 个答案:

答案 0 :(得分:0)

在清单中添加以下行:

  <uses-permission android:name="android.permission.CAMERA"/>

如果您的设备是Marshmallow,那么您需要为使用相机提供运行时权限。

https://developer.android.com/training/permissions/requesting.html