This was answered once, but was an incomplete answer. Could someone please try t
ID: 3740605 • Letter: T
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 that will be necessary. Thanks!
ANDROID STUDIO/JAVA
Exercise 10-4 Move a task to an asychronous thread
In this exercise, you’ll 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 that’s 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 can’t 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<Void, Void, String> {
@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:
Explanation / Answer
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<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try{
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 "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());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.