android – Cannot resolve symbol 'context'-ThrowExceptions

Exception or error:

I’m trying to write an Android app which automatically uploads a picture to a server, but I am stuck on just one line of code:

        File f = File(context.getCacheDir(), "filename");

The error I get is
enter image description here

This puzzles me because I see so many examples on the web of people using context.getCacheDir() just fine, whereas I get the error message.
It’s probably something wrong with my IDE settings. I am using IntelliJ IDE.

Here’s is the context of the context problem:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if( requestCode == CAMERA_PIC_REQUEST)
        {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
            image.setImageBitmap(thumbnail);

            //create a file to write bitmap data
            File f = File(context.getCacheDir(), "filename");
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
How to solve:

You need to do some basic Java programming tutorials. Java is totally different to JavaScript.

Here, you use context as a variable but you have neither declared it, or initialised it, hence the error.

You could define it (and initialise at the same time)

 Context context = this;

since this refers to the current object instance of a class and Activity is a Context, or more precisely, it extends Context.

Alternatively, you could just use this.

File f = File(UploadToServer.this.getCacheDir(), "filename");

###

The error is bacuse you havent declared context, neither it has been passed as a parameter

change context.getCacheDir() to getApplicationContext.getCacheDir() or this.getCacheDir()

so

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if( requestCode == CAMERA_PIC_REQUEST)
    {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
        image.setImageBitmap(thumbnail);

        //create a file to write bitmap data
        File f = File(context.getCacheDir(), "filename");
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

will become

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if( requestCode == CAMERA_PIC_REQUEST)
    {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
        image.setImageBitmap(thumbnail);

        //create a file to write bitmap data
        File f = File(getApplicationContext.getCacheDir(), "filename");
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

###

Add this :

Context context = this;

You need this to initialize context

Then Alt+Enter

You must assign context to this Activity

Leave a Reply

Your email address will not be published. Required fields are marked *