在我的应用程序中,有一个按钮和一个ImageView。点击按钮后,它从服务器获取照片并显示在inmageView上。在从服务器获取照片时,它会显示进度条。
此应用程序可以从服务器获取照片但无法在imageview上显示。
代码(用于按钮):
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonFetchPhoto:
new BackgroundTask().execute("Main");
break;
}
}
代码(进度条)
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();
}
}
由于“ imageViewGetPhoto.setImageBitmap(bm)”
,我收到错误我认为这是因为“ imageViewGetPhoto ”被定义为布局类,它是主线程,并且该线程无法与主线程同步。
如何将数据导入由其他线程检索的主线程。
或者我如何将对象访问到主布局中定义的其他线程。
错误:
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)
答案 0 :(得分:2)
更新
中的图片视图 @Override
protected void onPostExecute(Void result) {
//update your imageview
}
我们无法在工作线程中更新imageview。
答案 1 :(得分:1)
将imageViewGetPhoto.setImageBitmap(bm);
这样的ui移动到onPostExecute
..
protected void onPostExecute(Void result)
{
bar.dismiss();
imageViewGetPhoto.setImageBitmap(bm);
}
答案 2 :(得分:1)
在
中写下这3行@Override
protected void onPostExecute(Void result)
{
bar.dismiss();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
imageViewGetPhoto.setImageBitmap(bm);
}