Question
This was answered once, but was an incomplete answer. Could someone please try to answer this again with a complete answer including all the files
This was answered once, but was an incomplete answer. Could someone please try to answer this again with a complete answer including all the files that will be necessary. Thanks!
ANDROID STUDIO/JAVA
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started