In Android development, I am loading a drawable from a url with this code:
public Drawable getImageFromURL(String sURL) {
Object content = null;
try {
URL url = new URL(sURL);
content = url.getContent();
} catch (Exception ex) {
ex.printStackTrace();
}
InputStream is = (InputStream) content;
return Drawable.createFromStream(is, "src");
}
But I want to display this ProgressBar while it loads the drawable:
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(TabMenu.this, getString(R.string.name), getString(R.string.specials));
progressBar.setIcon(R.drawable.icon_template);
How do I do that? I call getImageFromURL() in the onCreate() methode of the Activity.
Thanks for your help!
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// do something
switch (msg.what){
case 1:
//ALL FUNCTION(S) -----------------------------
//-----------------------------------------------------
//-----------------------------------------------------
p_dialog.dismiss();
break;
// if error
case 0:
Toast.makeText(aktiviti.this, "Error", Toast.LENGTH_SHORT).show();
p_dialog.dismiss();
break;
}
}
};
ON CREATE ---------> <----------------
Button new_button = (Button) findViewById(R.id.new_button);
new_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
p_dialog = ProgressDialog.show((activity_name).this, "", "Loading Image...");
new Thread(new Runnable() {
public void run() {
processThread();
}}).start();
}
private void processThread() {
try{
handler.sendEmptyMessage(1);
}
catch (Exception e){
handler.sendEmptyMessage(0);
}
}
}
});
Good Luck!
You need to download the image in a new thread. You can use a AsyncTask for simplicity as in this example which implements also the progressbar
Related
I'm work on crate server and android client
but thread doesn't interrupted by android client
My android Client has request for the RoomList from server
and Server receive, Client Accept
and fill in my ListView used the Adapter
here's problem if Click backButton,
than RoomListAcitivity was close and thread was stop
but thread dosen't stop just alive in my app
first Enter has work on sucessfully
but Press BackButton on and re-Enter this Activity
MyApp just White, No Action
how can i stop this thread?
(and sorry for my English skill...)
i tried .interrupt() method , and handler.removeMessages(0)
but failed thread keep alive
upload this full java code just in case...
ListView roomList;
RoomAdapter roomAdapter;
Socketservice ss;
String msg,rtitle;
String msgs[];
String list[];
Thread listthread,EnterRoomThread,removeV;
boolean staterun = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_room);
roomList = findViewById(R.id.roomList);
roomAdapter = new RoomAdapter();
listthread = new Thread() {
public void run(){
ss.out.println("163|");
ss.out.println("100|");
try {
while (staterun == true) {
msg = ss.in.readLine();
handler.post(new Runnable() {
public void run() {
msgs = msg.split("\\|");
String protocol = msgs[0];
switch (protocol) {
case "163":
list = msgs[1].split(",");
for (int i = 0; i < list.length; i++) {
String list1[] = list[i].split("-");
String listT = list1[0];
int listC = Integer.parseInt(list1[1]);
int listI = Integer.parseInt(list1[2]);
roomAdapter.CreateRoom(listI, listT, listC);
}
roomList.setAdapter(roomAdapter);
msg = "";
msgs = null;
break;
case "200":
Intent i = new Intent(getApplicationContext(), GameWaitingActivity.class);
i.putExtra("tname", rtitle);
staterun = !staterun;
Toast.makeText(getApplicationContext(),""+listthread.isAlive(),Toast.LENGTH_SHORT).show();
startActivity(i);
finish();
break;
case "201":
Toast.makeText(getApplicationContext(), "방이 꽉 찼습니다.", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
} catch (IOException e) {
}
}
};
listthread.start();
roomList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Room item = (Room) roomList.getItemAtPosition(position);
rtitle=item.getTitle();
EnterRoomThread = new Thread() {
public void run() {
ss.out.println("200|" + rtitle);
EnterRoomThread.interrupt();
}
};
EnterRoomThread.start();
}
});
}
Handler handler = new Handler();
#Override
public void onBackPressed() {
removeV = new Thread() {
public void run(){
ss.out.println("101|");
removeV.interrupt();
}
};
removeV.start();
handler.removeMessages(0);
staterun = false;
listthread.interrupt();
Toast.makeText(getApplicationContext(),""+listthread.isAlive(),Toast.LENGTH_SHORT).show();
finish();
}
}
Go ahead with this, write this inside run() method
//use this boolean value to keep track.
boolean shouldRunFlag = true;
while (shouldRunFlag) {
try {
Thread.sleep(10);
//Do your work............
} catch (Exception e) {
e.printStackTrace();
Log.v(TAG, "Interrupted Exception caught");
// change the flag, so that while loop can be broken.
shouldRunFlag = false;
Log.v(TAG, "run: breaking the loop");
break;
}
}
and this is how you interrupt and clean the thread
private void interrupt(Thread currentThread) {
Log.i(TAG, "interrupt");
if (currentThread != null) {
Thread dummyThread = currentThread;
dummyThread.interrupt();
currentThread = null;
}
}
i am trying to show the progress dialog in my program when press the refresh button but the application close unexpexted with error in debugger that the thread which one is created ui hierachy can only touch it.
public class MainActivity extends Activity {
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.refreshView();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void refreshView(){
ImageView img = (ImageView) findViewById(R.id.imageView1);
Bitmap bm = null;
InputStream is = null;
BufferedInputStream bis = null;
try
{
URLConnection conn = new URL("http://technomoot.edu.pk/$Common/Image/Content/ciit_logo.jpg").openConnection();
conn.connect();
is = conn.getInputStream();
bis = new BufferedInputStream(is, 8192);
bm = BitmapFactory.decodeStream(bis);
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if (bis != null)
{
try
{
bis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
img.setImageBitmap(bm);
}
public void onRefresh(View view) {
final ProgressDialog dialog = ProgressDialog.show(this,"Loading","Loading the image of the Day");
Thread th = new Thread(){
public void run(){
refreshView();
handler.post(new Runnable(){
public void run(){
dialog.dismiss();
}
}
);
}
};th.start();
}
}
You can't manipulate with UI from worker Thread! It's prohibited! If you want to update UI just use
runOnUiThread()
AsyncTask
So try it like this:
runOnUiThread(new Runnable() {
#Override
public void run() {
// do your work.
}
});
AsyncTask is more complex, also generic-type that provides some benefits like type control etc. You can look at example here:
Android application (performance and more) analysis tools -
Tutorial
You are calling a ui operation from a non ui thread, you can Use runOnUiThread..
or better use AsynchTask
public YourTask extends AsyncTask<Object,Object,Object>{ //change Object to required type
ProgressDialog dialog;
Context context;
public YourTask(Context context){
this.context=context;
}
protected void onPreExecute(Object o){
dialog = ProgressDialog.show(this,"Loading","Loading the image of the Day");
}
protected void doInBackground(Object o){
refreshView();
}
protected void onPostExecute(Object o){
img.setImageBitmap(bm);
dialog.dismiss();
}
}
*I have class which load some files and update the UI...It takes some times to view the result,... So I want to add a loading bar or progress bar. Some data has been passed by other activity(Bundle extras1 = getIntent().getExtras();). But I am downloading one image per item... I think that takes more time. Any one can help me?
This is my code:
public class ShowSelectedEvents extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewdetailsevents);
Bundle extras1 = getIntent().getExtras();
String eventTitle = extras1.getString("eventTitle");
final String address = extras1.getString("address");
String date = extras1.getString("date");
String time = extras1.getString("time");
final String fix = extras1.getString("fix");
final String mobile = extras1.getString("mobile");
final String web = extras1.getString("web");
final String mail = extras1.getString("mail");
String imageLink = extras1.getString("imageLink");
final String videoLink = extras1.getString("videoLink");
// Add item image
Bitmap bitMap = null;
try {
bitMap = DownloadImage("imageLink);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageView imageItem = (ImageView) findViewById(R.id.imageItem);
imageItem.setImageBitmap(bitMap);
TextView viewTitle = (TextView) findViewById(R.id.viewTitle);
viewTitle.setText(eventTitle);
TextView viewDateTime = (TextView) findViewById(R.id.dateTime);
viewDateTime.setText("Event is on "+date +" # "+ time);
// View Address
TextView viewAdd = (TextView) findViewById(R.id.viewAddress);
viewAdd.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
viewAdd.setText(address);
// On click open Navigation
if (!(address.equals("-"))){
ImageView navigationIcon = (ImageView) findViewById(R.id.imageMapNavigation);
navigationIcon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "google.navigation:q="+address;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
});
}else{
Context context = getApplicationContext();
CharSequence text = "Address is incomplete!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
// View Phone number
TextView viewPhoneNumber = (TextView) findViewById(R.id.viewPhoneNumber);
viewPhoneNumber.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
viewPhoneNumber.setText(fix);
// On click open call
if (!(fix.equals("-"))){
viewPhoneNumber.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "tel:"+fix;
Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(url));
startActivity(i);
}
});
}
//View Mobile number
TextView viewMobileNumber = (TextView) findViewById(R.id.viewMobileNumber);
viewMobileNumber.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
viewMobileNumber.setText(mobile);
// on click call mobile number
if (!(mobile.equals("-"))){
viewMobileNumber.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "tel:"+mobile;
Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(url));
startActivity(i);
}
});
}
//View web url
TextView viewWeb = (TextView) findViewById(R.id.viewWeb);
viewWeb.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
viewWeb.setText(web);
//on click open web browser
if (!(web.equals("-"))){
viewWeb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(web));
startActivity(i);
}
});
}
TextView viewMail = (TextView) findViewById(R.id.viewMailAddress);
viewMail.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
viewMail.setText(mail);
if(!(mail.equals("-"))){
viewMail.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {mail});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Contact from tamilpage.ch");
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Send a mail ..."));
}
});
}
// On click play the video
if (!(address.equals("-"))){
ImageView videoIcon = (ImageView) findViewById(R.id.videoButton);
videoIcon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(videoLink));
startActivity(browserIntent);
}
});
}else{
Context context = getApplicationContext();
CharSequence text = "No advert video for this event";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
private Bitmap DownloadImage(String url) throws Exception {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(url);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
private InputStream OpenHttpConnection(String url) throws Exception {
InputStream in = null;
int response = -1;
System.out.println("Nishi1");
URL url1 = new URL(url);
URLConnection conn = url1.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
System.out.println(in);
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
}
So I want to add a loading bar or progress bar
For your approach you need to use Threads. Especially i recommend to you have look at
Handler
AsyncTask
Both approaches offer work with Threads. AsyncTask is more complex than Handler also it's generic-type so offer more type-safe and faster work.
You should read some tutorials so
ProgressBar updating using Message Handler
Create A Custom Progress Bar Using
AsyncTask
And there is very awesome and useful tutorial at Vogella
Android Threads, Handlers and AsyncTask -
Tutorial
I would look at using AsyncTask. Here is a link that should help you get started: http://developer.android.com/reference/android/os/AsyncTask.html
The way you're going to want to do this is within an ASyncTask that has callbacks for updating a progress bar. I've included a sample below that doesn't actually do anything. You'd put your code for calling the download in doInBackground() and update the progress from onProgressUpdate.
public class BackgroundAsyncTask extends
AsyncTask<Void, Integer, Void> {
int myProgress;
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Toast.makeText(AndroidAsyncTaskProgressBar.this,
"onPostExecute", Toast.LENGTH_LONG).show();
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
Toast.makeText(AndroidAsyncTaskProgressBar.this,
"onPreExecute", Toast.LENGTH_LONG).show();
myProgress = 0;
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
while(myProgress<100){
myProgress++;
publishProgress(myProgress);
SystemClock.sleep(100);
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
progressBar.setProgress(values[0]);
}
}
the Problem I am having is that the PostExecute is not firing.
I see the log tag for background but P.E. never fires.
I am invoking this task from a timer like this:
findViewById(R.id.buttonstart).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
openFile("FeedTimerTask.html");
Timer t = new Timer("FeedTimerTask", true);
timerTask = new FeedTimerTask();
t.schedule(timerTask, 2000, 20000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Runnable runme = new Runnable() {
#Override
public void run() {
timestart = Calendar.getInstance().getTimeInMillis();
provider.refreshNoCache();
}
};
class FeedTimerTask extends TimerTask{
#Override
public void run() {
try{Looper.prepare();}catch(Exception e){};
runme.run();
}
}
Here is the main task itself from inside the dataprovider class invoked with "provider.refreshNoCache();" above:
// threaded rteftesh tasks
#SuppressWarnings("rawtypes")
public class RefreshTask extends SupportAsyncTask {
private int errorcodecode = 0;
private ProgressDialog dialog=null;
private Exception mainExeption=null;
protected String waitMessage = "Laddar ner information..";
private boolean useCache;
public RefreshTask(boolean useCache) {
this.useCache = useCache;
}
public void onPreExecute() {
data = null;
if (showSpinnerOnRefresh){
dialog = ProgressDialog.show(context, "", waitMessage , true);
dialog.show();
}
}
protected Object doInBackground(Object... params) {
errorcodecode = 1;
try {
invokeFeedRead();
Log.e("DataProvider", "Bkgtask...");
errorcodecode = 0;
} catch (BrJSONException e) {
Log.e("[ERROR]","PROVIDER "+e.getMessage());
mainExeption = e;
errorcodecode = 1;
} catch (IOException e) {
Log.e("[ERROR]","PROVIDER "+e.getMessage());
mainExeption = e;
errorcodecode = 2;
} catch (Exception e) {
Log.e("[ERROR]","PROVIDER "+e.getMessage());
mainExeption = e;
errorcodecode = 3;
}
if (errorcodecode==0){
}
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
Log.e("DataProvider", "Cancelled...");
if (dialog != null)
try{dialog.dismiss();}catch(Exception e){}
BrAlert.Show(context, "Obs", BrAppConfig.ServerError+" (timeout)", 0);
onError_IO(new IOException("Timeout!"));
errorcodecode=2;
}
#Override
protected void onPostExecute(Object result) {
// super.onPostExecute(result);
Log.e("DataProvider", "PostExec...");
if (dialog != null)
try{dialog.dismiss();}catch(Exception e){}
switch (errorcodecode) {
case 0:
onFeedLoaded();
cacheAge = System.currentTimeMillis();
break;
case 1:
onError_DataFormat(mainExeption);
break;
case 2:
onError_IO(mainExeption);
break;
default:
onError_GeneralExeption(mainExeption);
}
}
}
Your task is cancelled even before it reached onPostExecte method. If the task is cancelled before it reaches onPostExecute Method. It will not trigger onPostExecute but trigger onCancelled Method. Please provide enough time to finish the task.
I found out the problem in the end. It was to do with the scope.
I needed a handler to invoke the other thread.
Here is the solution for others may find helpful:
in on create:
tickHandler = new Handler();
tickTimer = new Timer();
tickTimer.schedule(new FeedTimerTask(),
0,
50000); //FPS
The handler class.
class FeedTimerTask extends TimerTask{
private Runnable runable;
public FeedTimerTask(){
super();
runable = new Runnable(){
#Override
public void run() {
timestart = Calendar.getInstance().getTimeInMillis();
provider.refreshNoCache();
}
};
}
#Override
public void run() {
tickHandler.post(runable);
}
}
I am displaying data from sqlite db. The data in the database is stored in html. The data contains an image tag to display an image. I am able to display the data but how can I display the image along with it? My images are stored in the server.
My Code:
((TextView)view.findViewById(R.id.tv_text)).setText(Html.fromHtml(listItem.gettext()+""));
From the above line I am displaying the data from database. Now along with that I need to display the image which is in server. How can I do this?
Try my code . Hope it helps
TextView mDetail;
ProgressBar mProgressBar;
String body;
Handler handler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_recipe_detail);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (this.getIntent().getStringExtra("body") != null)
body = this.getIntent().getStringExtra("body");
mDetail = (TextView) this.findViewById(R.id.tv_recipe_detail);
if (this.getIntent().getStringExtra("body") != null)
body = this.getIntent().getStringExtra("body");
mDetail.setText(Html.fromHtml(body));
mProgressBar = (ProgressBar) this.findViewById(R.id.bar);
mDetail.setMovementMethod(ScrollingMovementMethod.getInstance());// 皛
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what == 0x101) {
mProgressBar.setVisibility(View.GONE);
mDetail.setText((CharSequence) msg.obj);
}
super.handleMessage(msg);
}
};
Thread t = new Thread(new ImageTask());
t.start();
mProgressBar.setVisibility(View.VISIBLE);
}
class ImageTask implements Runnable {
Message msg = Message.obtain();
#Override
public void run() {
ImageGetter imageGetter = new ImageGetter() {
#Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
URL url;
Drawable drawable = null;
try {
url = new URL(source);
drawable = Drawable.createFromStream(
url.openStream(), null);
int scale_y = MAX_HEIGHT/drawable.getIntrinsicHeight();
drawable.setBounds(0, 0,
drawable.getIntrinsicWidth()*scale_y,
drawable.getIntrinsicHeight()*scale_y);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException" + e.toString());
} catch (IOException e) {
Log.e(TAG, "IOException" + e.toString());
}
return drawable;
}
};
CharSequence test = Html.fromHtml(body, imageGetter, null);
msg.what = 0x101;
msg.obj = test;
handler.sendMessage(msg);
}
}