I am working on an application where i have to implement a button, and when the button is clicked, an image download starts and a progressDialog appears containing a horizontal bar that shows the image download progress. When the download is done, the progress dialog disappears and the image is shown. The problem is that when the download is going on (progressDialog is shown) , if i rotate my screen, the app crashes, how can i solve this problem please ?
This is my code :
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivityProgress extends Activity {
Button button;
private ProgressDialog progressDialog;
ImageView imageView;
public static final int progress_bar_type = 0;
private static String file_url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_progress);
button = (Button) findViewById(R.id.btnProgressBar);
imageView = (ImageView) findViewById(R.id.my_image);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadFileFromURL().execute(file_url);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Downloading file. Please wait...");
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.show();
return progressDialog;
default:
return null;
}
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main_activity_progress);
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@SuppressLint("SdCardPath")
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(total*100)/lenghtOfFile);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
progressDialog.setProgress(Integer.parseInt(progress[0]));
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String file_url) {
dismissDialog(progress_bar_type);
String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
imageView.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
}
These are my logcat errors :
07-31 10:00:48.023: E/MoreInfoHPW_ViewGroup(20485): Parent view is not a TextView
07-31 10:00:49.658: E/MoreInfoHPW_ViewGroup(20485): Parent view is not a TextView
07-31 10:00:49.728: E/ViewRootImpl(20485): sendUserActionEvent() mView == null
07-31 10:00:50.753: E/MoreInfoHPW_ViewGroup(20485): Parent view is not a TextView
07-31 10:00:50.803: E/ViewRootImpl(20485): sendUserActionEvent() mView == null
07-31 10:00:52.688: E/MoreInfoHPW_ViewGroup(20485): Parent view is not a TextView
07-31 10:00:52.803: E/ViewRootImpl(20485): sendUserActionEvent() mView == null
07-31 10:00:52.803: E/ViewRootImpl(20485): sendUserActionEvent() mView == null
07-31 10:00:52.828: E/ViewRootImpl(20485): sendUserActionEvent() mView == null
07-31 10:01:01.018: E/AndroidRuntime(20485): FATAL EXCEPTION: main
07-31 10:01:01.018: E/AndroidRuntime(20485): Process: com.example.progressdownload, PID: 20485
07-31 10:01:01.018: E/AndroidRuntime(20485): java.lang.IllegalArgumentException: no dialog with id 0 was ever shown via Activity#showDialog
07-31 10:01:01.018: E/AndroidRuntime(20485): at android.app.Activity.missingDialog(Activity.java:3214)
07-31 10:01:01.018: E/AndroidRuntime(20485): at android.app.Activity.dismissDialog(Activity.java:3199)
07-31 10:01:01.018: E/AndroidRuntime(20485): at com.example.progressdownload.MainActivityProgress$DownloadFileFromURL.onPostExecute(MainActivityProgress.java:121)
07-31 10:01:01.018: E/AndroidRuntime(20485): at com.example.progressdownload.MainActivityProgress$DownloadFileFromURL.onPostExecute(MainActivityProgress.java:1)
07-31 10:01:01.018: E/AndroidRuntime(20485): at android.os.AsyncTask.finish(AsyncTask.java:632)
07-31 10:01:01.018: E/AndroidRuntime(20485): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-31 10:01:01.018: E/AndroidRuntime(20485): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
07-31 10:01:01.018: E/AndroidRuntime(20485): at android.os.Handler.dispatchMessage(Handler.java:102)
07-31 10:01:01.018: E/AndroidRuntime(20485): at android.os.Looper.loop(Looper.java:157)
07-31 10:01:01.018: E/AndroidRuntime(20485): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-31 10:01:01.018: E/AndroidRuntime(20485): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 10:01:01.018: E/AndroidRuntime(20485): at java.lang.reflect.Method.invoke(Method.java:515)
07-31 10:01:01.018: E/AndroidRuntime(20485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-31 10:01:01.018: E/AndroidRuntime(20485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-31 10:01:01.018: E/AndroidRuntime(20485): at dalvik.system.NativeStart.main(Native Method)
Thanks.