2014-07-07 124 views
0

我已經從URL加載圖像的問題GridView控件從imageview的成功已經但是 從GridView控件加載圖像URL加載圖像到gridview的

負荷圖像URL不起作用

ScreenShoot

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

確實像這樣ImageLoader.getInstance()。displayImage(imageUrl,imageView); //默認選項將被使用 –

+0

檢查我的答案:http://stackoverflow.com/questions/19651291/image-from-url-in-android/19651443#19651443 –

回答

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文件,設置名稱值在應用程序代碼這一類。例如:

<application 
    android:name="com.example.myproject.UILApp" 
    android:allowBackup="true" 
    android:icon="@drawable/app_icon" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

- 第三,cretate您ImageLoader的對象是這樣的:

DisplayImageOptions options = new DisplayImageOptions.Builder() 
     .cacheOnDisk(true) 
     .build(); 

ImageLoader loader = ImageLoader.getInstance(); 

在上面的代碼,我把cacheing在磁盤上如此。 DisplayImageOptions有許多其他設置可以檢出並設置。我已經創建了我的加載器對象作爲類變量,所以我可以在課程中的任何地方使用它。欲瞭解更多信息,請訪問GitHub上的Universal Image Loader頁面:https://github.com/nostra13/Android-Universal-Image-Loader

希望這有助於!