0

在我的应用程序中有一个按钮和一个 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)
4

3 回答 3

2

更新您的图像视图

 @Override
 protected void onPostExecute(Void result) {    
      //update your imageview 
 }

我们无法在工作线程中更新 imageview。

于 2012-12-11T11:59:51.307 回答
1

将ui之类的任何修改移动imageViewGetPhoto.setImageBitmap(bm);onPostExecute..

protected void onPostExecute(Void result) 
{   
    bar.dismiss();
    imageViewGetPhoto.setImageBitmap(bm);
  }
于 2012-12-11T11:53:30.900 回答
1

将这 3 行写入

@Override
    protected void onPostExecute(Void result) 
    { 
        bar.dismiss();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        imageViewGetPhoto.setImageBitmap(bm); 

     }
于 2012-12-11T11:53:49.213 回答