1

私はアンドロイド開発の初心者で、カメラで撮影した画像を返すのに苦労しています。縦向き/縦向きの写真を撮るとアプリは正常に動作しますが、横向き/横向きの写真を撮ると、結果の配信に失敗したというエラーでアプリがクラッシュします 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);
}

ログキャット

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

アプリで縦表示と横表示をサポートしますか? そうでない場合は、向きの変更を防止することで問題を解決できる可能性があります。onCreate に次のコードを追加します。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

カメラ インテントで方向を変更することは引き続き可能です。

于 2013-04-03T13:46:08.990 に答える
2

画面の向きが変わると、Activity は破棄され、新しい向きで再作成されます。そのため、Activity の存続期間中に参照を取得した変数は、もはや何も参照しなくなります。その後、値を再割り当てせずにそれらが参照するオブジェクトにアクセスしようとすると、NullPointerException が発生します。

メソッド onSaveInstanceState() は、設定変更間の一時データを保存するために使用されます。これにより、Activity の再起動時に onCreate() に渡される Bundle が作成されます。

コードがなければ、それがあなたの問題かどうかはわかりませんが、一見の価値があります。

詳細については、 http://developer.android.com/reference/android/app/Activity.html#ConfigurationChangesを参照してください (間違いなく、私が提供したよりも正確な情報です)。

他の投稿からコピーしたものではありません

于 2013-04-03T13:07:48.750 に答える
1

私は以前にこの問題を抱えていましたが、私の原因に一致する回答はありませんでした。

問題は次のとおりです。

  1. カメラが引き継ぐ
  2. カメラ モードでデバイスが回転している
  3. 写真を撮って確認する
  4. 写真はアクティビティに送り返されますが、アクティビティ/フラグメントは破棄され、出力URIファイル/文字列を保存した変数を含む画面の向きの変更で再作成されます

解決:

  1. onSaveInstanceStateアクティビティの再作成に使用されるバンドルを含むメソッドに出力 URI を保存します。
  2. onRestoreInstanceStateアクティビティとonCreateViewフラグメントのバンドルからファイルを取得します
于 2014-12-17T10:10:29.403 に答える