Load images in grid view from Url

南笙酒味 提交于 2021-02-20 03:38:34

问题


i'm trying to fill my gridview using URL links declared on xml : the problem is that when i run the application ; i get an empty gridview.

MainActivity.java

public class MainActivity extends Activity {
private GridView gridView;
private GridViewAdapter customGridAdapter;
TypedArray imgs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new  StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    gridView = (GridView) findViewById(R.id.gridView);
    customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
    gridView.setAdapter(customGridAdapter);
 }

 private ArrayList getData() {
    final ArrayList imageItems = new ArrayList();
    // retrieve String drawable array
     imgs = getResources().obtainTypedArray(R.array.image_ids);
    this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < imgs.length(); i++) {
                Drawable drawable = LoadImageFromWebOperations(imgs.getString(i));

                Log.i("TAG","URL de l'image : "+imgs.getString(i));
                Log.i("TAG","contenu du drawable : "+drawable);


                imageItems.add(new ImageItem(drawable,"Image  : "+i));
                Log.i("TAG","image Items : "+imageItems);
            }

        }
    });


    return imageItems;

 }
 private Drawable LoadImageFromWebOperations(String url)
 {
    try
    {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
     }catch (Exception e) {
         System.out.println("Exc="+e);
        return null;
     }
 }

}

GridViewAdapter

 public class GridViewAdapter extends ArrayAdapter<ImageItem> {
 private Context context;
 private int layoutResourceId;
 private ArrayList<ImageItem> data = new ArrayList<ImageItem>();

 public GridViewAdapter(Context context, int layoutResourceId,
                       ArrayList<ImageItem> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.imageTitle = (TextView) row.findViewById(R.id.text);
        holder.image = (ImageView) row.findViewById(R.id.image);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    ImageItem item = data.get(position);
    holder.imageTitle.setText(item.getTitle());
    holder.image.setImageDrawable(item.getImage());
    return row;
 }

 static class ViewHolder {
    TextView imageTitle;
    ImageView image;
 }
 }

array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="image_ids">
    <item>http://media-cdn.tripadvisor.com/media/photo-s/02/83/a6/00/mon-port-hotel-spa.jpg</item>
    <item>http://media-cdn.tripadvisor.com/media/photo-s/02/83/a6/00/mon-port-hotel-spa.jpg</item>
    <item>http://media-cdn.tripadvisor.com/media/photo-s/02/83/a6/00/mon-port-hotel-spa.jpg</item>
    <item>http://media-cdn.tripadvisor.com/media/photo-s/02/83/a6/00/mon-port-hotel-spa.jpg</item>


</string-array>

</resources>

row_grid.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:padding="5dp"
android:clickable="true"
android:focusable="true">

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp" >
</ImageView>

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:textSize="12sp" >
</TextView>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
tools:context=".MainActivity" >

<GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:columnWidth="100dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:verticalSpacing="5dp"
    android:drawSelectorOnTop="true"
    android:stretchMode="columnWidth" >
</GridView>

</RelativeLayout>

回答1:


PROBLEM SOLVED ; i added just after setContentView(R.layout.activity_main); on MainActivity

if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }


来源:https://stackoverflow.com/questions/23894572/load-images-in-grid-view-from-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!