Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol 'context'

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();
            }
like image 650
hexicle Avatar asked Feb 16 '14 09:02

hexicle


1 Answers

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");
like image 62
Simon Avatar answered Sep 21 '22 09:09

Simon