The Android page jumps, and the previous page is destroyed, resulting in an error

Description of the problem: There is a button on the A page, select the gallery photo to crop, and then display it, a simple function, an error is reported on a Mi 9 mobile phone. The cropped photo keeps prompting: "An error occurred while saving, and the save failed . "

Problem analysis: First of all, this prompt is given by the system, so we gave the system a wrong message, which caused the system to give such a prompt. There is definitely nothing wrong with the step of selecting a picture, because we did not give the system any data in this step, and there was an error when we should crop the picture, because in this step, we need to provide a path that should be saved after the cropping, and the cropping code is:

crop(data.getData(), getOutCropUri());

    private Uri getOutCropUri() {
      
        Debug.i(TAG, "preCroppedPath:" + preCroppedPath);

        File outFile = new File(preCroppedPath);
        File parentFile = outFile.getParentFile();
        if (parentFile != null) {
            if (!parentFile.exists()) {
                Debug.i(TAG, "getOutCropUri :parent: is not exists");
                parentFile.mkdirs();
            }
        }
        return Uri.fromFile(outFile);
    }

 
private void crop(Uri inUri, Uri outUri) {
        if (inUri == null) {
            return;
        }
        Debug.i(TAG, inUri + "crop" + outUri);
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(inUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 2);
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
        intent.putExtra("noFaceDetection", true);
        startActivityForResult(intent, REQ_CROP);
    }

I printed out the path, the path is - ": file:///null/dial_bg.png, you can see that there is a null in the middle of the path. Why is it null, I can't understand it. On other mobile phones, that is, the normal path is: file:/ //storage/emulated/0/Android/data/XXX/files/watch_skin_local/ca8200bd5cf1b1e6f5f70794116adff3 /dial_bg.png, the red part can be seen because The member variable, that is, preCroppedPath is empty. This variable is assigned in oncreate. I can make it clear that this path is not empty before selecting a photo. Why, after selecting a photo, this member variable is empty. I took a closer look at the log and found that not only this member variable was empty, but all member variables became empty. I looked again and found that the page A was completely destroyed when it jumped to the selection album. Damn, it's really crazy, is the app memory leak so serious? Insufficient memory when jumping to the page, causing the resources of the previous page to be reclaimed. For verification, I created a new project with only two pages, and A jumped to B. Then I found that even in this case, after the B page is successfully created, A will still be onDestory. Damn, I'm going crazy, this phone's configuration is too low? Are you sure it's Mi 9? A battle angel who claims to be good-looking and capable of fighting? ? ? Smash it, rubbish the phone, destroy it, don't love it anymore. After drinking a cup of tea and calming down, I feel that it is not a problem with the configuration of the mobile phone. Could it be that something has been turned on in the developer options, causing the previous interface to be destroyed every time a new page is opened. So, being smart, I went to the developer options and took a look, hey, what is this, "do not keep activities" activity = activity? fall! closed! ! ! So run it again, all right. I'm sorry, Mr. Lei, I misunderstood you, and I'm sorry for Mi 9. After a closer look, this option is available in the developer options of each mobile phone.

So far, the cause of the problem has been found, and of course it can be solved by turning off this option, but the customer is God, you can tell your God, you are not allowed to open this button. Can't. Therefore, we need to save the data that needs to be saved in onSaveInstanceState, and then take it out in oncreate.


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preCroppedPath=savedInstanceState==null?null:savedInstanceState.getString("data");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("data",preCroppedPath);
    }

So far the problem is really solved.

Daily records:

Before you know it, it's the end of the year. In my natal year, I had a smooth life. To be precise, this year has been a year of mediocrity. I didn't buy a house, my salary didn't increase, and I didn't have a date. I was crying. The biggest breakthrough was to participate in Trailwalking activities. I walked for more than 10 hours and walked 50km. It ruined my life for a long time. I also learned kotlin, and the functions are basically implemented with kotlin. It still feels very useful. It's very comfortable to use. But I still have to say, java is the best language. I don’t know if it’s because I’m getting older, my tears are getting lower and lower, and I’m talking less and less, and I like to be alone more and more. My friends said that I have entered the old age in advance, ah ha ha ha, I think this is a A slow life. I still like this kind of life very much, this year's summary is like this, generally speaking, 3 stars. Next year, I hope to learn a language and get a raise in salary! ! ! come on! ! !

Single cycle "Shepherd Song"

Guess you like

Origin blog.csdn.net/androidzmm/article/details/103764082