Search code examples
androidpicassoairplane-mode

Picasso sometimes not download photos and crash my application


Picasso.with(getActivity().getApplicationContext()).load(imageString).resize(250, 250)
    .into(image, new Callback() {
        @Override
        public void onSuccess() {
            Log.e("profilepicsucess", "");
        }

        @Override
        public void onError() {
            Log.e("profilepicfalse :3", "");
        }
});

When I try to download photo using Picasso, SOMETIMES my application crashed WITHOUT accessing onSuccess, onError Functions! and I have this in my logcat

(W/Settings﹕ Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.)

I searched for it I found that I should import :

import static android.provider.Settings.System.AIRPLANE_MODE_ON;

and write this function

static boolean isAirplaneModeOn(Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    return Settings.System.getInt(contentResolver, AIRPLANE_MODE_ON, 0) != 0;
}

Actually I don't know WHERE to write it**


Solution

  • The reason you are getting the warning is because from Jelly Bean 4.2 and up, the airplane mode settings have moved to Settings.Global.

    Use this function to check the airplane mode:

    /**
     * Gets the state of Airplane Mode.
     * 
     * @param context
     * @return true if enabled.
     */
    @SuppressWarnings("deprecation")
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static boolean isAirplaneModeOn(Context context) {        
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.System.getInt(context.getContentResolver(), 
                    Settings.System.AIRPLANE_MODE_ON, 0) != 0;          
        } else {
            return Settings.Global.getInt(context.getContentResolver(), 
                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
        }       
    }
    

    However, I would need more detail to assist with the crashes.

    This is how to use this function:

    if(!isAirplaneModeOn(getActivity().getApplicationContext()){   
        //Picasso Code
        Picasso.with(getActivity().getApplicationContext()).load(imageString).resize(250, 250)
            .into(image, new Callback() {
                @Override
                public void onSuccess() {
                    Log.e("profilepicsucess", "");
                }
    
                @Override
                public void onError() {
                    Log.e("profilepicfalse :3", "");
                }
        });
    }else{
        //do something else?
    }