我已經可以從服務器下載 的圖像,但直到它完成下載圖像, 我使用這個代碼的應用程序不加載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;
}
}
非常感謝的人,運行非常大:
相反,你應該從任務的
onPostExecute
方法設置圖像 –