我遇到从网址到网格视图的加载图片的问题
从imageview加载图片网址已经成功 但是,从gridview加载图像网址不起作用
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
请帮帮我......
答案 0 :(得分:0)
如果您使用通用图像加载程序显示图像,请检查您是否正确完成了以下步骤。如果没有,下面的步骤也将提供您使用它的基本教程:
- 创建一个扩展应用程序的类,并将下面的代码放入其中。在这种情况下,我将我的类命名为UILApp:
package com.example.myproject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import android.app.Application;
public class UILApp extends Application{
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext()).build();
ImageLoader.getInstance().init(configuration);
}
}
- 第二,在AndroidManifest.xml文件中,将应用程序标记中的name值设置为此类。示例:
<application
android:name="com.example.myproject.UILApp"
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
-Third,像这样创建你的ImageLoader对象:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.build();
ImageLoader loader = ImageLoader.getInstance();
在上面的代码中,我将缓存放在磁盘上。 DisplayImageOptions还有许多其他可以检出和设置的设置。我已经将我的loader对象创建为类变量,因此我可以在类中的任何位置使用它。有关更多信息,请访问GitHub上的Universal Image Loader页面:https://github.com/nostra13/Android-Universal-Image-Loader
希望这有帮助!