摄像头捕获后文件对象变为null的原因

时间:2013-11-10 23:30:14

标签: android file

我的代码

public class MainActivity extends Activity {

        private View TakePhotoBtn;

        private ImageView ProductPhoto;

        private Button ReviewBtn;

        public static File file;

        private static final int CAMERA_REQUEST = 1888;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
              .....

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
        }

        void TakePhoto() {

                Intent cameraIntent = new Intent(
                                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                file = new File(Environment.getExternalStorageDirectory()
                                + File.separator + "test.png");
                Uri imageUri = Uri.fromFile(file);

                cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                                imageUri);
                BugTrackerApp.app.file =file ;

                startActivityForResult(cameraIntent, CAMERA_REQUEST);

        }

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

                if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {



// this line is getting null value .... why 

                        file =  BugTrackerApp.app.file ;
                        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                         ProductPhoto.setImageBitmap(bitmap);
                         ReviewAct.ImageFile = file;

                }
        }

}

2 个答案:

答案 0 :(得分:1)

据推测,当您在后台时,您的过程已被终止。不要将静态数据成员依赖于缓存。

答案 1 :(得分:0)

你对BugTrackerApp做了什么?你检查过了吗? 你为什么需要两条线?

BugTrackerApp.app.file =file ;
file =  BugTrackerApp.app.file ;

您创建了一个文件来存储捕获的图像,并在onActivityResult中对其进行解码。所以这一行

file = new File(Environment.getExternalStorageDirectory()
                                + File.separator + "test.png");

就够了。