getApplicationContext() outside onCreate to upload an image

霸气de小男生 提交于 2021-01-29 08:27:24

问题


I want to upload a image to the server. But I need to send with it the username from the user that is sending the image.

I have the username record in SharedPreferences, so I think I could get it:

public class UploadRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://160.128.0.10/up.php";
    private Map<String, String> params;

    public UploadRequest(String image, String name, Response.Listener<String> listener){
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);

        SharedPreferences pref = getApplicationContext().getSharedPreferences("pref01", MODE_PRIVATE); 
// CANNOT RESOLVE SYMBOL getApplicationContext
        String user = pref.getString("username", null);

        params = new HashMap<>();
        params.put("image",image);
        params.put("name",name);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

is it wrong? how can I get this username?


回答1:


What you need to do is, just pass the context of the main activity to this class by its constructor. So, create constructor as:

public UploadRequest(Context context, String image, String name, Response.Listener<String> listener){
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);

    SharedPreferences pref = context.getSharedPreferences("pref01", MODE_PRIVATE); 
// CANNOT RESOLVE SYMBOL getApplicationContext
    String user = pref.getString("username", null);

    params = new HashMap<>();
    params.put("image",image);
    params.put("name",name);
}

And when you initialize it from your activity, pass this argument as the context of the activity. Something like this:

UploadRequest ur = new UploadRequest(this, OTHER_PARAMETERS_HERE);

As mentioned in comments by others, your code is not working because getApplicationContext() is a function and you can call it if and only if the object has that function defined.




回答2:


getApplicationContext() can only be called from a subclass of Context, Activity is one of them which is why you can call getApplicationContext() from your Activity.

What you need to do is either have a global Context initialised in your Application class (wouldn't recommend) or pass in Context as a parameter to this class.




回答3:


Your are not allowed to use getApplicationContext() so easily anywhere in android. so the error is perfectly alright from android point of view.

Do not use context references anywhere mainly in case of network calls. Many times contexts are tied to Ui(Activity). You will run into difficult times of NullPointersExpetions.

Solution :

Pass your username as parameter.

public class UploadRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://160.128.0.10/up.php";
private Map<String, String> params;

public UploadRequest(String image, String usernName,String name, Response.Listener<String> listener){
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);

    SharedPreferences pref = getApplicationContext().getSharedPreferences("pref01", MODE_PRIVATE); 
// CANNOT RESOLVE SYMBOL getApplicationContext
    String user = pref.getString("username", null);

    params = new HashMap<>();
    params.put("image",image);
    params.put("name",name);
}

@Override
public Map<String, String> getParams() {
    return params;
}
}


来源:https://stackoverflow.com/questions/42351354/getapplicationcontext-outside-oncreate-to-upload-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!