Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ANDROID PROGRAMMING PLEASE I NEED AN EXPERT ANSWER This is the 3rd time I have submitted this. Exercise 10-4 Move a task to an asychronous

ANDROID PROGRAMMING PLEASE I NEED AN EXPERT ANSWER

This is the 3rd time I have submitted this.

Exercise 10-4 Move a task to an asychronous thread

In this exercise, youll modify an app that downloads an image file from the Internet, writes that image to a file, and reads that image from the file.

Review and test the app

Start Android Studio and open the project named ch10_ex4_TestAsync.

Review the code for this app. Note that it uses an asynchronous task to download an image file from the Internet.

Run the app. When it starts, it should briefly display the standard Android image. Then, it should download another image from the Internet, write it to a file, read that file, and display the image in an ImageView widget.

Move a task from the UI thread to another thread

Create an asynchronous class named ReadFile that you can use to read and display the image file thats stored on the device.

Move all code within the readFile method to the doInBackground method of the ReadFile class. Then, delete the readFile class and clean up the code so there are no syntax errors.

Run the app. You should get an error that indicates that you cant update the UI thread from the background thread.

Modify the declaration of the ReadFile class so it returns a Drawable object.

Modify the doInBackground method so it returns a Drawable object.

Modify the onPostExecute method so it uses the Drawable object to display the image.

Run the app. It should work as before. However, the app now uses a separate thread to read the image file.

package com.murach.ch10_ex4; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.Log; import android.widget.ImageView; public class MainActivity extends Activity { private final String URL_STRING = "http://www.murach.com/images/andp.jpg"; private final String FILENAME = "android_book.jpg"; private ImageView fileImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fileImageView = (ImageView) findViewById(R.id.fileImageView); new DownloadFile().execute(); } class DownloadFile extends AsyncTask { @Override protected String doInBackground(Void... params) { // download and write the file try{ // get the URL object URL url = new URL(URL_STRING); // get the input stream InputStream in = url.openStream(); // get the output stream FileOutputStream out = openFileOutput(FILENAME, Context.MODE_PRIVATE); // read input and write output byte[] buffer = new byte[1024]; int bytesRead = in.read(buffer); while (bytesRead != -1) { out.write(buffer, 0, bytesRead); bytesRead = in.read(buffer); } out.close(); in.close(); // return a message return "File downloaded"; } catch (IOException e) { return "Error: " + e.toString(); } } @Override protected void onPostExecute(String message) { Log.d("Test", message); readFile(); } } private void readFile() { try { FileInputStream in = openFileInput(FILENAME); Drawable image = Drawable.createFromStream(in, FILENAME); fileImageView.setImageDrawable(image); Log.d("Test", "File read"); } catch (Exception e) { Log.e("Test", "Error: " + e.toString()); } } }

This is the layout xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ImageView android:id="@+id/fileImageView" android:layout_width="220dp" android:layout_height="220dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:contentDescription="@string/file_image" android:src="@drawable/ic_launcher" /> RelativeLayout>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

List all the courses that have prerequisites ( using EXISTS )

Answered: 1 week ago