ProgressDialog dismiss is not working - android

I have a ProgressDialog set up but i ca not dismiss it! it just keep loading the message and wont end. I think I am doing it right, set up the show message in onPreExecute and dismiss it into onPostExecute but some how it's just not working
heres my code:
public class MainActivity extends Activity {
//String[] imgUrls = {getString(R.string.uncc_main_thumb),getString(R.string.football_main_thumb,getString(R.string.ifest_main_thumb),getString(R.string.commencement_main_thumb))};
String[] imgUrls={"http://farm5.staticflickr.com/4113/4843614620_c541de5a5c_m.jpg",
"http://farm8.staticflickr.com/7265/8151547298_85e60e7368_m.jpg",
"http://farm9.staticflickr.com/8054/8414075899_e87a74407b_m.jpg",
"http://farm9.staticflickr.com/8210/8277674769_7d1245dbf1_m.jpg"};
ProgressDialog progressDialog;
ImageView iv1,iv2,iv3,iv4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 =(ImageView) findViewById(R.id.imageView1);
iv2 =(ImageView) findViewById(R.id.imageView2);
iv3 =(ImageView) findViewById(R.id.imageView3);
iv4 =(ImageView) findViewById(R.id.imageView4);
for (String imgUrl : imgUrls) {
new DownLoadImages().execute(imgUrl);
}
}
private class DownLoadImages extends
AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... params) {
String imgUrl = params[0];
Bitmap image = null;
try {
URL url = new URL(imgUrl);
image = BitmapFactory.decodeStream(url.openStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
// show the loading message while downloading the image
ProgressDialog.show(MainActivity.this, null, "Loading");
}
#Override
protected void onPostExecute(Bitmap result) {
if (result == null) {
result = ((BitmapDrawable) getResources().getDrawable(
R.drawable.not_found)).getBitmap();
}
//ImageView imageViewToBeAdded = new ImageView(getBaseContext());
// imageViewToBeAdded.setLayoutParams(new LayoutParams(
// LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// imageViewToBeAdded.setPadding(5, 5, 5, 5);
iv1.setImageBitmap(result);
progressDialog.dismiss();
}
}
}

You are not creating instance of ProgressDialog.
Replace
ProgressDialog.show(MainActivity.this, null, "Loading");
With
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading");

Use progressDialog instead of ProgressDialog to call show method of ProgressDialog as:
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(true);
progressDialog.setMessage("Loading...");
issue occurring due to you are calling show method using ProgressDialog but trying to dismiss using progressDialog instance.

In onPreExecute you use the className ProgressDialog.show, which is a static method, in onPostExecute you are actually using your object to stop it.
Use your object to start it in onPreExecute and it will work.

Instantiate your dialog and save the reference:
progressDialog = ProgressDialog.show(MainActivity.this, null, "Loading");

Related

How to get Uri of an image from url?

I want to get uri of an image from url that is stored on server.
I have tried this.
uri = new URI("http://www.google.com/");
and i have also tried this
riuri = Uri.parse( "http://www.facebook.com" );
Do you want to save the image file in your device or what u want exactly.
ImageView.setImageURI(Uri.parse("http://www.facebook.com"));
or u need to save it in your phone .
// DownloadImage AsyncTask
private class DownloadImage extends AsyncTask {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Download Image Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
#Override
protected Bitmap doInBackground(String... URL) {
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
image.setImageBitmap(result);
// Close progressdialog
mProgressDialog.dismiss();
}
}

how run asynctask inside asynctask in the same time?

i have an web-based application that must retrive text in external database and image in server after one text retrived ,using following code for retrive text:
class BackgroundTaskOne extends AsyncTask<Void, Void, String>
{
String json_url;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
json_url="http://..some url../somefile.php";
}
#Override
protected String doInBackground(Void... voids) {
// TODO Auto-generated method stub
String error="";
try {
URL url=new URL(json_url);
HttpURLConnection httpUrlConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream=httpUrlConnection.getInputStream();
BufferedReader bufferReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder=new StringBuilder();
while((JSON_STRINO=bufferReader.readLine())!=null){
stringBuilder.append(JSON_STRINO+"\n");
//add new textView
}
bufferReader.close();
inputStream.close();
httpUrlConnection.disconnect();
json_string=stringBuilder.toString().trim();
return stringBuilder.toString().trim();
// return "one row of data inserted..";
} catch (MalformedURLException e) {
error=e.getMessage()+" first";
} catch (IOException e) {
// TODO Auto-generated catch block
error=e.getMessage()+" sec";
}
return error;
}
#Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
json_string=result;
l=parse();
int i=0;
for(final String a[]:l){
TextView t=new TextView(MainActivity.this);
final String path=a[2]+".jpg";
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
t.setText(a[0]+" : "+a[1]+" i="+i);
t.setId(i);
Reklam.addView(t);
new LoadImage().execute(path);
i++;
}}}
i want to load image while loading first text and second image after second text in sequence but it loads all text then load all images , these folowing code for loading image:
private class LoadImage extends AsyncTask<String, String, Bitmap> {
Bitmap bitmap;
ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
ImageView img=new ImageView(MainActivity.this);
img.setImageBitmap(image);
img.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Reklam.addView(img);
pDialog.dismiss();
}else{
pDialog.dismiss();
Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
if you want to run multiple thread parallely :
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
new MyAsyncTask().execute();
}

Image downloading is not working in android

I am trying to download the image from the URL http://dbh_cache.s3.amazonaws.com/19445/34173cb38f07f89ddbebc2ac9128303f-33b64a2ed0f1ff4750f183b4f2a161b8.png
It seem the domain of the URL contains underscore which results in image download failure. Please let me know if i am correct
public class LoadImage extends AsyncTask<String, String, Bitmap> {
ImageView img;
Bitmap bitmap;
Context con;
ProgressDialog pDialog;
public LoadImage(Context updateProfileActivity,
ImageView profileImageView) {
// TODO Auto-generated constructor stub
con = updateProfileActivity;
img = profileImageView;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(con);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
img.setImageBitmap(image);
pDialog.dismiss();
}else{
pDialog.dismiss();
// Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}

How to set wallpaper of image getting from url in android

From below code I am able to get the image on imageview from server using URL of that image. Now I want to set as wallpaper of that image. Please provide solution.... so that I will be able to set wallpaper without downloading the image in my phone.
onCreate() method
{
image = (ImageView) findViewById(R.id.image);
new DownloadImage().execute(URL);
}
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity1.this);
mProgressDialog.setTitle("Downloading....");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Bitmap doInBackground(String... URL) {
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
image.setImageBitmap(result);
// Close progressdialog
mProgressDialog.dismiss();
}
}
WallpaperManager wpm = WallpaperManager.getInstance(context);
InputStream ins = new URL("absolute/path/of/image").openStream();
wpm.setStream(ins);
you should add permission for this
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>

Progress Dialog should spin till loading of gridview completed

I am working on one android app in which i want to display progress Dialog till loading of gridview completed. But my problem is progress dialog is spin for some intial time. Then it stops spinning.
Here is my code.
public class allsites extends Activity {
private final String url_select = "http://api.stackexchange.com/2.1/sites?filter=!RGB_Y51.*-(YX";
private GridView gview;
private ListViewCustomAdapter adapter;
private ArrayList<Object> itemList = new ArrayList<Object>();
private ItemBean bean;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.allsites);
//GridView gridview = (GridView) findViewById(R.id.gvAllSites);
gview = (GridView) findViewById(R.id.gvallsites);
new task().execute();
}
private class task extends AsyncTask<Void, Void, GZIPInputStream> {
private ProgressDialog progress;
#Override
protected void onPreExecute() {
progress = ProgressDialog.show(allsites.this, "Loading", "Please Wait...");
}
#Override
protected GZIPInputStream doInBackground(Void... params) {
ServerData httpclient = new ServerData();
GZIPInputStream zis = httpclient.GetServerData(url_select);
return zis;
}
#Override
protected void onPostExecute(GZIPInputStream zis) {
ParseJSON(zis);
if(progress!=null && progress.isShowing()==true)
progress.dismiss();
}
}
private void ParseJSON(GZIPInputStream zis)
{
Gson gson = new Gson();
Reader reader = new InputStreamReader(zis);
Sites response = gson.fromJson(reader, Sites.class);
List<Items> items = response.getItems();
for (Items site : items) {
//Toast.makeText(allsites.this, site.getApi_site_parameter().toString(), Toast.LENGTH_SHORT).show();
AddObjectToList(site.getIcon_url(),site.getName());
}
adapter = new ListViewCustomAdapter(this, itemList);
gview.setAdapter(adapter);
}
public void AddObjectToList(String imageURL, String title)
{
bean = new ItemBean();
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
bean.setImage(bitmap);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bean.setTitle(title);
itemList.add(bean);
}
}
Please give me suggestion how i can make progress dialog spinning till gridview get loaded.
move ParseJSON function to doInBackground event
#Override
protected Boolean doInBackground(Void... params) {
ServerData httpclient = new ServerData();
GZIPInputStream zis = httpclient.GetServerData(url_select);
ParseJSON(zis);
return true;
}
#Override
protected void onPostExecute(Boolean zis) {
progress.dismiss();
}

Categories

Resources