Reputation: 591
I am trying to download images from URL to and SD card. To perform this task I added the Picasso library in my app and tried this code to download images to the SD card:
package com.example.imagedownloadsample;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_download);
Button bn = (Button) findViewById(R.id.button1);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something to download images to sd card
File file = new File(Environment.getExternalStorageDirectory(),
"Android/data/com.usd.pop");
file.mkdirs();
Picasso.with(context).load("your URL").into(file);
}
});
}
}
but I got this error:
Description Resource Path Location Type context cannot be resolved to a variable MainActivity.java /ImageDownloadSample/src/com/example/imagedownloadsample line 31 Java Problem
Upvotes: 0
Views: 3296
Reputation: 2147
A dynamic way :
Picasso.with(getApplicationContext()).load("your URL").into(file);
Upvotes: 1
Reputation: 24848
Try to replace this code,hope this will help you to solve your problem.
public class MainActivity extends ActionBarActivity {
private Context context;
private Button bn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_download);
context = this;
bn = (Button) findViewById(R.id.button1);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something to download images to sd card
File file = new File(Environment.getExternalStorageDirectory(),
"Android/data/com.usd.pop");
file.mkdirs();
Picasso.with(context).load("your URL").into(file);
}
});
}
}
Upvotes: 0