我对这个应用程序有问题应该是简单的修复,但我无法弄清楚我到底做错了什么...所有的应用程序都是下载图像在一个线程中,并将图像设置在另一个线程中。我改变了ReadFile类,将一个Drawable对象返回到变量finalImage,然后我有onPostExecute将该图像设置为ImageView但是......它唯一显示的是Android图标。任何建议,将不胜感激。我猜测它与我从ReadFile类返回Drawable的方式有关,但我无法弄清楚到底是什么。
package com.example.imageviewer;
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://cdn.theatlantic.com/static/infocus/putin091311/s_p15_0RTXUL0R.jpg";
private final String FILENAME = "s_p15_0RTXUL0R.jpg";
private ImageView fileImageView;
public Drawable finalImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fileImageView = (ImageView) findViewById(R.id.fileImageView);
new DownloadFile().execute();
new ReadFile().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);
fileImageView.setImageDrawable(finalImage);
}
}
public class ReadFile extends AsyncTask <Void, Void, Drawable>{
@Override
protected Drawable doInBackground(Void...params){
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());
}
return finalImage;
}
}
}
答案 0 :(得分:0)
你应该返回&#34;图像&#34;而不是&#34; finalImage&#34;来自ReadFile任务。
此外,您可以尝试使用此代替onPostExecute:
public class MainActivity extends Activity implements AsyncResponse {
...
void processFinish(Drawable output) {
fileImageView.setImageDrawable(finalImage);
}
}
AsyncResponse.java:
public interface AsyncResponse {
void processFinish(Drawable output);
}
ReadFile.java:
public class ReadFile extends AsyncTask <Void, Void, Drawable>{
@Override
protected Drawable doInBackground(Void...params){
Drawable image;
try {
FileInputStream in = openFileInput(FILENAME);
image = Drawable.createFromStream(in, FILENAME);
Log.d("Test", "File read");
}
catch (Exception e) {
Log.e("Test", "Error: " + e.toString());
}
return image;
}
@Override
protected void onPostExecute(Drawable result) {
delegate.processFinish(result);
}
}
答案 1 :(得分:0)
你的问题在这里:
new DownloadFile().execute();
new ReadFile().execute();
由于这些是异步任务,它们会同时运行,而ReadFile
正在尝试在DownloadFile
完成之前打开您的图像文件。
您只需将new ReadFile().execute();
移至onPostExecute()
的{{1}}方法。
然后还将以下内容添加到DownloadFile
,以使ImageView刷新:
ReadFile
我进行了测试以确保所需要的一切,并得到普京与狗的照片。