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
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();
}
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With