عابد محمد فتال عابد محمد فتال - 29 days ago 27
Android Question

i have slow UI in my android app

I've an activity that can download an image from sever
but the app not loading the UI until it's finishing downloading the image ,
I'm using this code:

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;
}
}

Answer

You don't properly use the AysncTask class. By calling get(), you pause your main thread until the image is downloaded. That's why the UI feels slow.

Instead, you should set the image from the task's onPostExecute method:

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);
    }
}