1

我是 android 开发的菜鸟,我无法返回相机拍摄的图像。当我垂直/纵向拍照时,我的应用程序运行良好,但是当我水平/横向拍照时,应用程序崩溃并出现错误,显示失败交付结果 ResultInfo{who=null, request=1, result=-1, data=null } 。我已经调整了输出图片的宽度和高度,但没有成功。任何帮助是极大的赞赏。

我的代码

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        //check if we are returning from picture selection
        if (requestCode == PICKER) {

            //the returned picture URI
     //       Uri pickedUri = data.getData();

            //declare the bitmap
            Bitmap pic = null;
            //declare the path string
            String imgPath = "";

            //retrieve the string using media data
            String[] medData = { MediaStore.Images.Media.DATA };
            //query the data
            Cursor picCursor = managedQuery(outputFileUri, medData, null, null, null);
            if(picCursor!=null)
            {
                //get the path string
                int index = picCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                picCursor.moveToFirst();
                imgPath = picCursor.getString(index);
            }
            else
                imgPath = outputFileUri.getPath();

            //if and else handle both choosing from gallery and from file manager

            //if we have a new URI attempt to decode the image bitmap
            if(outputFileUri!=null) {

                //set the width and height we want to use as maximum display
                int targetWidth = 600;
                int targetHeight = 400;

                //sample the incoming image to save on memory resources

                //create bitmap options to calculate and use sample size
                BitmapFactory.Options bmpOptions = new BitmapFactory.Options();

                //first decode image dimensions only - not the image bitmap itself
                bmpOptions.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(imgPath, bmpOptions);

                //work out what the sample size should be

                //image width and height before sampling
                int currHeight = bmpOptions.outHeight;
                int currWidth = bmpOptions.outWidth;

                //variable to store new sample size
                int sampleSize = 1;

                //calculate the sample size if the existing size is larger than target size
                if (currHeight>targetHeight || currWidth>targetWidth) 
                {
                    //use either width or height
                    if (currWidth>currHeight)
                        sampleSize = Math.round((float)currHeight/(float)targetHeight);
                    else 
                        sampleSize = Math.round((float)currWidth/(float)targetWidth);
                }
                //use the new sample size
                bmpOptions.inSampleSize = sampleSize;

                //now decode the bitmap using sample options
                bmpOptions.inJustDecodeBounds = false;

                //get the file as a bitmap
                pic = BitmapFactory.decodeFile(imgPath, bmpOptions);

                if(currentPic<=10){                     
                    //pass bitmap to ImageAdapter to add to array
                    imgAdapt.addPic(pic);
                    currentPic++;
                }

                //redraw the gallery thumbnails to reflect the new addition
                picGallery.setAdapter(imgAdapt);

                //display the newly selected image at larger size
                picView.setImageBitmap(pic);
                //scale options
                picView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            }
        }
    }
    //superclass method
    super.onActivityResult(requestCode, resultCode, data);
}

LOGCAT

04-03 08:26:46.113: E/AndroidRuntime(15318): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.pickenssurvey/com.example.pickenssurvey.PictureGallery}: java.lang.NullPointerException
04-03 08:26:46.113: E/AndroidRuntime(15318):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3179)
04-03 08:26:46.113: E/AndroidRuntime(15318):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2534)
04-03 08:26:46.113: E/AndroidRuntime(15318):    ... 13 more
04-03 08:26:46.113: E/AndroidRuntime(15318): Caused by: java.lang.NullPointerException
04-03 08:26:46.113: E/AndroidRuntime(15318):    at android.content.ContentResolver.acquireProvider(ContentResolver.java:913)
04-03 08:26:46.113: E/AndroidRuntime(15318):    at android.content.ContentResolver.query(ContentResolver.java:305)
04-03 08:26:46.113: E/AndroidRuntime(15318):    at android.app.Activity.managedQuery(Activity.java:1742)
04-03 08:26:46.113: E/AndroidRuntime(15318):    at com.example.pickenssurvey.PictureGallery.onActivityResult(PictureGallery.java:241)
04-03 08:26:46.113: E/AndroidRuntime(15318):    at android.app.Activity.dispatchActivityResult(Activity.java:4723)
04-03 08:26:46.113: E/AndroidRuntime(15318):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3175)
4

3 回答 3

2

当您的屏幕方向发生变化时,您的 Activity 将被销毁并以新的方向重新创建。因此,在 Activity 生命周期内获得引用的任何变量都将不再引用任何内容,如果您随后尝试访问它们引用的对象而不重新分配它们的值,您将获得 NullPointerException。

onSaveInstanceState() 方法用于在配置更改之间保存临时数据。这将创建一个 Bundle,当 Activity 再次启动时传递给 onCreate()。

没有任何代码,我不知道这是否是您的问题,但值得一看。

请参阅http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges了解更多信息(毫无疑问,我提供的信息比我提供的更准确)。

不是我从其他帖子复制的

于 2013-04-03T13:07:48.750 回答
2

您想在您的应用程序中支持纵向和横向视图吗?如果不是,则可以通过防止方向更改来解决问题。将以下代码放入您的 onCreate 中:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

仍然可以更改相机意图中的方向。

于 2013-04-03T13:46:08.990 回答
1

我之前遇到过这个问题,没有看到任何与我的原因相匹配的答案。

问题是:

  1. 相机接管
  2. 设备在相机模式下旋转
  3. 拍照并确认
  4. 图片被发送回活动,但您的活动/片段在屏幕方向更改时被销毁并重新创建,包括存储输出 URI 文件/字符串的变量

解决方案:

  1. 将输出 URI 存储在onSaveInstanceState包含用于重新创建活动的包的方法中
  2. 从 Bundle 中检索文件以onRestoreInstanceState获取活动和onCreateView片段
于 2014-12-17T10:10:29.403 回答