In my application one button and one ImageView is there. After clicking on button it get the photo from server and display on inmageView. At the time of getting photo from server it show progress bar.
This application able to get photo from server but not able to display on imageview.
code (for button):
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonFetchPhoto:
new BackgroundTask().execute("Main");
break;
}
}
code(for progress bar)
class BackgroundTask extends AsyncTask<String , Integer, Void>
{
@Override
protected void onPreExecute()
{
bar = new ProgressDialog(activity.this);
bar.setMessage("Processing..");
bar.setIndeterminate(true);
bar.show();
}
@Override
protected Void doInBackground(String... params)
{
// Code to get Photo Byte Array from server
Bitmap bm = BitmapFactory.decodeByteArray(photoByteArray, 0,photoByteArray.length);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
imageViewGetPhoto.setImageBitmap(bm); //imageViewGetPhoto is define in activity class
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
bar.dismiss();
}
}
I am getting error because of “imageViewGetPhoto.setImageBitmap(bm)“
I think this is because “imageViewGetPhoto” is defined into the layout class which is main thread and this thread not able to synchronize with main thread.
How I get the data into Main thread which is retrieved by other thread.
Or how I access objects into other thread which is defined in main layout.
Error:
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRoot.checkThread(ViewRoot.java:3020)
at android.view.ViewRoot.requestLayout(ViewRoot.java:634)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.widget.ScrollView.requestLayout(ScrollView.java:1580)
at android.view.View.requestLayout(View.java:8267)
at android.widget.TableLayout.requestLayout(TableLayout.java:226)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.widget.TableLayout.requestLayout(TableLayout.java:226)
at android.view.View.requestLayout(View.java:8267)
at android.view.View.requestLayout(View.java:8267)
at android.widget.ImageView.setImageDrawable(ImageView.java:322)
at android.widget.ImageView.setImageBitmap(ImageView.java:336)
at com.pixel.potholelocator.PohholeLocatorActivity$BackgroundTask.doInBackground(PohholeLocatorActivity.java:295)
at com.pixel.potholelocator.PohholeLocatorActivity$BackgroundTask.doInBackground(PohholeLocatorActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
Write these 3 lines in