我需要帮助。我正在尝试使用 glide 从服务器获取图像,但我不知道如何?请构建我的代码,以便 Glide 正常工作或为我提供任何其他可能的解决方案。我只想用 Glide 和 JSON 显示图像列表视图,JSON 结果来自我的 PHP 脚本。除了图像之外,我还可以获取其他数据。这是我的代码。我应该把滑翔机放在哪里?
public class list extends ListActivity {
private static final String TAG_POST= "posts";
private static final String TAG_ID="lecID";
private static final String TAG_NAME="lecName";
private static final String TAG_EMAIL="lecEmail";
private static final String TAG_PASS="lecPass";
private static final String TAG_IMAGE="image";
private static final String LECTURER_LIST_URL="http://system.com/list.php";
private JSONArray mComments =null;
private ArrayList<HashMap<String,String>> mcommentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
ImageView imageView=(ImageView)findViewById(R.id.lec_Image);
//upload is the folder to store my image
String leciv="http://fypesystem.com/upload/";
Glide.with(this).load(leciv)
.thumbnail(0.5f)
.into(imageView);
非常感谢您的帮助。
public static void LoadImage(Context cx,ImageView imageView,int demoImage,String url) {
if (!((Activity) cx).isFinishing()){
Glide.with(cx)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder(demoImage)
.into(imageView);
}
}
cx 是上下文 demoImage 是在加载图像之前显示的图像,Url 是从 JSON 获取的图像路径。
您必须创建一个自定义适配器,并向该适配器传递您从 json 创建的列表和要用于适配器的布局以及您的活动的上下文。 之后,如果您使用的是 listview,而不是在 listview 的 getView() 函数中添加此代码,其中 url 应该是您存储在列表中的值,就像这样
String url = mcommentList.get(position).get(TAG_IMAGE);
myImageView
应该是您传递给适配器的布局中的图像
Glide.with(context)
.load(url)
.placeholder(R.drawable.ic_launcher)
.into(myImageView);
您可以查看此链接寻求帮助
https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
更改这些行,特别是 URL。
ImageView imageView=(ImageView)findViewById(R.id.lec_Image);
//upload is the folder to store my image
String leciv="http://fypesystem.com/upload/9c419dd5659b0af4d14f642f454dcb07.jpg";
Glide.with(this).load(leciv)
.thumbnail(0.5f)
.into(imageView);