Android如何在片段中显示URL中的图像
ImageView(来自URL的图像)无法显示在片段中(表示空白屏幕) 在活动页面中显示是正确的,但当我更改为Fragement时,它根本无法显示。有什么问题请告诉我Android如何在片段中显示URL中的图像,android,Android,ImageView(来自URL的图像)无法显示在片段中(表示空白屏幕) 在活动页面中显示是正确的,但当我更改为Fragement时,它根本无法显示。有什么问题请告诉我 import java.io.IOException; import java.lang.ref.WeakReference; import java.net.MalformedURLException; import java.net.URL; import android.graphics.Bitmap; import an
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
public class ImageLoad extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
public ImageLoad(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
protected Bitmap doInBackground(String... params) {
url = params[0];
try {
return BitmapFactory.decodeStream(new URL(url).openConnection()
.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
protected void onPostExecute(Bitmap result) {
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(result);
}
}
}
protected void onPreExecute() {
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageResource(R.drawable.no_image);
}
}
}
}
import java.io.IOException;
导入java.lang.ref.WeakReference;
导入java.net.MalformedURLException;
导入java.net.URL;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.os.AsyncTask;
导入android.widget.ImageView;
公共类ImageLoad扩展异步任务{
私有字符串url;
私有最终WeakReference imageViewReference;
公共图像加载(图像视图图像视图){
imageViewReference=新的WeakReference(imageView);
}
受保护位图doInBackground(字符串…参数){
url=params[0];
试一试{
返回BitmapFactory.decodeStream(新URL.openConnection()
.getInputStream());
}捕获(格式错误){
e、 printStackTrace();
返回null;
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
}
受保护的void onPostExecute(位图结果){
if(imageViewReference!=null){
ImageView=imageViewReference.get();
如果(imageView!=null){
设置图像位图(结果);
}
}
}
受保护的void onPreExecute(){
if(imageViewReference!=null){
ImageView=imageViewReference.get();
如果(imageView!=null){
imageView.setImageResource(R.drawable.no_图像);
}
}
}
}
修复了它,因为我的变量传递错误。您的示例正在活动中使用。我想要的是碎片。
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
public class ImageLoad extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
public ImageLoad(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
protected Bitmap doInBackground(String... params) {
url = params[0];
try {
return BitmapFactory.decodeStream(new URL(url).openConnection()
.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
protected void onPostExecute(Bitmap result) {
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(result);
}
}
}
protected void onPreExecute() {
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageResource(R.drawable.no_image);
}
}
}
}