Reputation: 412
i am making a gallery app and when i execute my asynctask in Getview of Adapter class then only onPreExecution part of Asynctask gets execute and others don't.
So, what i see is a progress bar But not the image after downloading...on my activity just a progress bar keeps on rolling
Here is my code of Getview method
enter public View getView(int position, View convertview, ViewGroup parent) {
// TODO Auto-generated method stub
img=new ImageView(mContext);
new DownloadImage().execute();
return img;
}
and this is my Asynctaskclass
private class DownloadImage extends AsyncTask<String, Void, Bitmap>{
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(mContext);
// Set progressdialog title
mProgressDialog.setTitle("Download Image");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Bitmap doInBackground(String... URL) {
// TODO Auto-generated method stub
String imageURL = URL[0];
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
// Set the bitmap into ImageView
img.setImageBitmap(result);
// Close progressdialog
mProgressDialog.dismiss();
}
}
I think Just right after executing onPreExecution my GetView method return img variable to my MainActivity class..here it is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setTitle("My Gallery");
GridView g=(GridView)findViewById(R.id.gallery);
// Gallery g=(Gallery)findViewById(R.id.gallery);
// ImageAdapter width=new ImageAdapter(context);
g.setAdapter(new ImageAdapter(this));
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
GRID_PADDING, r.getDisplayMetrics());
g.setNumColumns(NUM_OF_COLUMN);
g.setColumnWidth(columnWidth);
g.setStretchMode(GridView.NO_STRETCH);
g.setPadding((int) padding, (int) padding, (int) padding,
(int) padding);
// g.setHorizontalSpacing((int) padding);
// g.setVerticalSpacing((int) padding);
g.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),ImageViewPager.class);
i.putExtra("id", position);
startActivity(i);
}
});
Can anybody tell me how to proceed?
Upvotes: 0
Views: 973
Reputation: 44571
You shouldn't be running your AsyncTask
in getView()
. Since it is asynchronous, getView()
won't wait for it to return. It will return a blank ImageView
.
You should run the task before setting the adapter and get all of the images first.
You can also use some image loading library like Picaso. You can Google and find other libraries, as well, and how to use them. And you shouldn't be using StrictMode
.
To get the images before running getView()
Run the task in onCreate()
. Then simply set your Adapter
in onPostExecute()
of your task.
Upvotes: 2