我已經從URL加載圖像的問題GridView控件從imageview的成功已經但是 從GridView控件加載圖像URL加載圖像到gridview的
負荷圖像URL不起作用
HalamanUtama.Java
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class HalamanUtama extends android.app.Fragment {
public HalamanUtama() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_halaman_utama, container,
false);
try{
int loader = R.drawable.loader;
ImageView image = (ImageView) view.findViewById(R.id.banner);
String image_url = "https://www.google.com/images/srpr/logo11w.png";
ImageLoader imgLoader = new ImageLoader(getActivity().getApplicationContext());
imgLoader.DisplayImage(image_url, loader, image);
} catch (Exception e){}
GridView gridView = (GridView) view.findViewById(R.id.grid_view1);
gridView.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// Sending image id to FullScreenActivity
Intent i = new Intent(getActivity().getApplicationContext(), FullImage.class);
// passing array index
i.putExtra("id", arg2);
startActivity(i);
}
});
return view;
}
}
ImageAdapter.Java
import java.io.InputStream;
import java.net.URL;
import android.R.drawable;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mLayoutInflater;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7
};
public String[] titleProduct={
"satu", "dua", "tiga",
"empat","lima","enam",
"tujuh"
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
mLayoutInflater=LayoutInflater.from(c);
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));*/
//return imageView;
ViewHolder mVHolder;
if(convertView==null){
convertView=mLayoutInflater.inflate(R.layout.custom_grid, parent, false);
mVHolder=new ViewHolder();
mVHolder.mImageView=(ImageView)convertView.findViewById(R.id.imageProduct);
mVHolder.mTextView=(TextView)convertView.findViewById(R.id.titleProduct);
mVHolder.mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mVHolder.mImageView.setPadding(8,8,8,8);
convertView.setTag(mVHolder);
} else{
mVHolder=(ViewHolder) convertView.getTag();
}
try{
ImageLoader imgLoader=new ImageLoader(mContext);
imgLoader.DisplayImage("https://www.google.com/images/srpr/logo11w.png", R.drawable.loader, mVHolder.mImageView);
mVHolder.mTextView.setText(titleProduct[position]);
} catch (Exception e){}
return convertView;
}
static class ViewHolder{
ImageView mImageView;
TextView mTextView;
}
}
logcat的
07-07 12:05:41.419: D/AbsListView(26215): Get MotionRecognitionManager
07-07 12:05:41.449: D/AbsListView(26215): Get MotionRecognitionManager
07-07 12:05:41.479: D/dalvikvm(26215): GC_FOR_ALLOC freed 166K, 10% free 9404K/10375K, paused 23ms, total 24ms
07-07 12:05:41.539: W/ResourceType(26215): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)
07-07 12:05:41.559: W/ResourceType(26215): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)
07-07 12:05:41.669: D/libEGL(26215): loaded /system/lib/egl/libEGL_mali.so
07-07 12:05:41.690: D/libEGL(26215): loaded /system/lib/egl/libGLESv1_CM_mali.so
07-07 12:05:41.690: D/libEGL(26215): loaded /system/lib/egl/libGLESv2_mali.so
07-07 12:05:41.720: D/OpenGLRenderer(26215): Enabling debug mode 0
07-07 12:05:41.770: D/dalvikvm(26215): GC_CONCURRENT freed 126K, 10% free 9710K/10695K, paused 13ms+3ms, total 32ms
07-07 12:06:00.620: W/IInputConnectionWrapper(26215): getSelectedText on inactive InputConnection
07-07 12:06:00.620: W/IInputConnectionWrapper(26215): setComposingText on inactive InputConnection
請幫我...
確實像這樣ImageLoader.getInstance()。displayImage(imageUrl,imageView); //默認選項將被使用 –
檢查我的答案:http://stackoverflow.com/questions/19651291/image-from-url-in-android/19651443#19651443 –