Android的startCamera给我空意图和......它毁了我的全局变量?我的、给我、毁了、意图

2023-09-11 20:13:08 作者:叼着棒棒糖拽天下

我的下一个问题:

当我尝试启动我的相机,我可以拍摄照片,甚至将其保存在我的SD卡,但是当我要得到的路径显示在我的设备我得到错误的。

when I try to start my camera, I can take the picture, even save it on my sdcard, but when I'm going to get the path for showing it on my device I get errors.

我的全局变量2(我用了1,但2是确保这是一个奇怪的错误越好):

My global variables are 2 (I used 1 but 2 are better for making sure it's a strange error):

    private File photofile;
private Uri mMakePhotoUri;

这是我的启动摄像功能:

and this is my start-camera function:

@SuppressLint("SimpleDateFormat")
public void farefoto(int num){
// For naming the picture
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String n = sdf.format(new Date());
    String fotoname = "Immagine-"+ n +".jpg";

//Going through files and  folders
    File photostorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File photostorage2 = new File(photostorage, "Immagini");
    System.out.println(photostorage+"\n"+photostorage2);
    photostorage2.mkdirs();
// My file (global)
    photofile = new File(photostorage2, fotoname);
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //intent to start camera
// My URI (global)
    mMakePhotoUri = Uri.fromFile(photofile);
    new Bundle(); // I took this code from internet, but if I remove this line, it's the same
    i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
    startActivityForResult(i, num); //num would be 1 on calling function
}

和我activityresult:

and my activityresult:

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == 1){

            try{ // tring my global URI
                photo = f.decodeAndResizeFile(new File(mMakePhotoUri.getPath()));
            }
            catch(NullPointerException ex){
                System.out.println("fail");
                ex.printStackTrace();
                try{ // Trying my global FILE
                photo = BitmapFactory.decodeFile(photofile.getAbsolutePath());
                } catch (Exception e){
                    e.printStackTrace();
                    Toast.makeText(this, "C'è stato un errore. Riprova a scattare la foto.", Toast.LENGTH_LONG).show();
                }

......
......
.......
}

总是越来越NullPointerException异常

Always getting NullPointerException

不过... 如果我再次拍照,它的工作!

我读过的一切,我可以在这里...但它没有逻辑,当我修改了一个全局变量,我不能把它再...

I've read everything I could here... but it doesn't have logic when I modify a global variable and I cannot take it again...

在此先感谢。 干杯。

由于亚历克斯·科恩说,我的问题是,我是叫的onCreate onActivityResult 的,因为很可能推出的内存(因为有时候没有做到这一点),所以我想有我的应用程序健康和我尝试了一些的try / catch 等我拿到即使它调用数据的onCreate onActivityResult 的第一个电话,我写了一个包数据,如在restoring我们国家。

As Alex Cohn said, my problem was that I was calling onCreate before onActivityResult because of a probably push out of memory (because sometimes doesn't do it), so I wanted to have my app "healthy" and I tried some try / catch and so I get the data even if it's calling onCreate or onActivityResult for the first call, and I wrote that data in a Bundle like explained in the link of restoring our state.

谢谢!

推荐答案

这有可能是推出 ACTION_IM​​AGE_CAPTURE 将推动你的活动内存不足。您应该检查(我只是一个记录器,调试器可能有自己的副作用)的的onCreate()的活动是前 onActivityResult称为( )。如果是这样的话,你应该prepare你的活动重新初始化本身,可能使用的onSaveInstanceState(捆绑)

It's possible that launching of ACTION_IMAGE_CAPTURE will push your activity out of memory. You should check (I'd simply a log, debugger may have its own side effects) that onCreate() of your activity is called before onActivityResult(). If this is the case, you should prepare your activity to reinitialize itself, probably using onSaveInstanceState(Bundle).

请注意,无论关闭活动,或保持它在后台的决定,取决于整个系统的状态,这是无法控制的。它不会让我感到吃惊,如果当你拍摄第一张照片的决定,是把他关了!,但是当你再拍照,这是让他在后台。

Note that the decision whether to shut down the activity, or keep it in background, depends on the overall system state that is beyond your control. It won't surprise me if the decision when you take the first picture, is "shut him down!", but when you take picture again, it is "keep him in background".