2016-11-08 47 views
-2

我已經可以從服務器下載 的圖像,但直到它完成下載圖像, 我使用這個代碼的應用程序不加載UI的活動:我有緩慢的UI在我的Android應用

private Toolbar toolbar; 
private ImageView downloadedImg; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    downloadedImg = (ImageView) findViewById(R.id.Main_Image_ID); 
    toolbar = (Toolbar) findViewById(R.id.Main_Appbar_ID); 

    setSupportActionBar(toolbar); 

    getSupportActionBar().setDisplayShowHomeEnabled(true); 

    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) 
      getSupportFragmentManager().findFragmentById(R.id.Main_Activity_navigation_drawer_ID); 
    drawerFragment.setUp(R.id.Main_Activity_navigation_drawer_ID, (DrawerLayout) findViewById(R.id.Main_DrawerLayout_ID), toolbar); 


    ImageDownloader task = new ImageDownloader(); 
    Bitmap myImage; 

    try { 
     myImage = task.execute("https://drive.google.com/uc?id=0B51TujFYa0RBUUYzSE15WHZRR3c").get(); 

     downloadedImg.setImageBitmap(myImage); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    Log.i("Interaction", "Button Tapped"); 
} 

public void ViewMone(View view) { 
    startActivity(new Intent(MainActivity.this,MainWebViewActivity.class)); 
} 


public class ImageDownloader extends AsyncTask<String, Void, Bitmap> { 

    @Override 
    protected Bitmap doInBackground(String... urls) { 
     try { 
      URL url = new URL(urls[0]); 
      HttpURLConnection connection = (HttpsURLConnection) url.openConnection(); 
      connection.connect(); 

      InputStream inputStream = connection.getInputStream(); 
      //to convert Image from string data to bitmap 
      Bitmap myBitmap = BitmapFactory.decodeStream(inputStream); 

      return myBitmap; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

回答

2

你沒有正確使用AysncTask類。通過調用get(),您暫停主線程,直到下載圖像。這就是爲什麼UI感覺很慢。

private Toolbar toolbar; 
private ImageView downloadedImg; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    // ... omitted ... 

    ImageDownloader task = new ImageDownloader();  
    task.execute("https://drive.google.com/uc?id=0B51TujFYa0RBUUYzSE15WHZRR3c"); 

} 

// ... omitted ...  

public class ImageDownloader extends AsyncTask<String, Void, Bitmap> { 

    @Override 
    protected Bitmap doInBackground(String... urls) { 
     try { 
      URL url = new URL(urls[0]); 
      HttpURLConnection connection = (HttpsURLConnection) url.openConnection(); 
      connection.connect(); 

      InputStream inputStream = connection.getInputStream(); 
      //to convert Image from string data to bitmap 
      Bitmap myBitmap = BitmapFactory.decodeStream(inputStream); 

      return myBitmap; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(Bitmap result) { 

     downloadedImg.setImageBitmap(result); 
    } 
} 
+0

非常感謝的人,運行非常大:

相反,你應該從任務的onPostExecute方法設置圖像 –

0

您必須異步執行此操作,而不是在主要活動中執行此操作,否則該頁面將被阻止,直到下載完成。有很多選擇可以做到這一點。我認爲最好的,因爲在這個問題上的公認的答案表示:

Android : Loading an image from the Web with Asynctask

0

如果你看一下異步任務的文檔,你會發現,get()有如下描述

等待,如果計算完成所必需的,然後檢索其結果。

與其說get()你應該在你的AsyncTask子類中實現onPostExecute並處理圖像那裏。在android開發者頁面上甚至有一個例子。

public class ImageDownloader extends AsyncTask<String, Void, Bitmap> { 
    <...> 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     downloadedImg.setImageBitmap(result); 
    } 
} 
相關問題