我想创建一个动态ImageView,它从在线服务器下载图像并加载到应用程序中。我正在使用此tutorial
中的代码问题是代码正在处理我在布局文件中创建的静态ImageView,但是当我将它用于动态ImageView时却没有。它会加载加载器图像,但在下载到手机内存后不会替换为URL中的原始图像。它生成以下错误 -
D / skia:--- decoder-> decode返回false
这是我的ImageAdapter.java代码 -
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.pixelandpublish.shreeshasirestaurant.R;
import com.pixelandpublish.shreeshasirestaurant.activity.ProductsActivity;
import com.pixelandpublish.shreeshasirestaurant.model.Categories;
import java.util.List;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private DatabaseHandler db;
// Constructor
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
db = new DatabaseHandler(mContext);
List<Categories> categories = db.getAllCategories();
return categories.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(340, 340));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setId(position);
}
else
{
imageView = (ImageView) convertView;
}
//imageView.setImageResource(mThumbIds[position]);
db = new DatabaseHandler(mContext);
List<Categories> categories = db.getAllCategories();
final String[] log = new String[categories.size()];
final Integer[] id = new Integer[categories.size()];
int i = 0;
for (Categories cn : categories) {
log[i] = cn.getName();
id[i] = cn.getID();
i++;
}
// Loader image - will be shown before loading image
int loader = R.drawable.ic;
// Imageview to show
//ImageView image = (ImageView) findViewById(R.id.image);
// Image url
String image_url = "http://api.androidhive.info/images/sample.jpg";
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(mContext);
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(image_url, loader, imageView);
//imageView.setImageResource(mThumbIds[position]);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(mContext, ProductsActivity.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("categoryId", String.valueOf(id[position]));
//Add the bundle to the intent
i.putExtras(bundle);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.getApplicationContext().startActivity(i);
}
});
return imageView;
}
}
提前完成。 :)
答案 0 :(得分:1)
你可以简单地使用毕加索图书馆从互联网上下载图片......
http://code.tutsplus.com/tutorials/android-sdk-working-with-picasso--cms-22149
答案 1 :(得分:0)
您可以使用Univeral Image Loader ...请参阅此链接 Universal Image Loader
ImageLoader imageLoader = ImageLoader.getInstance();
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(activity));
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.YOUR_DRWABLE)
.showImageForEmptyUri(R.drawable.YOUR_DRWABLE)
.showImageOnFail(R.drawable.YOUR_DRWABLE)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
imageLoader.DisplayImage("YOUR URL", R.drawable.YOUR_DRWABLE, IMAGEVIEW_HERE);