图像URL未在网格中显示 [英] images url are not displaying in grid

查看:198
本文介绍了图像URL未在网格中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为图像url..I的GridView能不能看到在网格中的图像..只是节目的背景...并单击网格在下一屏幕播放。

什么即时通讯做错了什么?如何实现这一点?

感谢很多提前

我的code


 公共类的行为扩展活动{
静态字符串uri1 =htt​​ps://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg;
静态字符串uri2 =htt​​ps://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg;
静态字符串uri3 =htt​​ps://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg;
公共静态的String [] =网址{uri1,uri2,uri3};
公共位图位图;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    GridView控件GRD =(GridView控件)findViewById(R.id.gridView1);
    grd.setAdapter(新ImageAdapter(本));
    grd.setOnItemClickListener(新OnItemClickListener()
    {
    公共无效onItemClick(适配器视图<>母公司,视图V,INT POS,长I​​D)
    {
        Toast.makeText(getBaseContext(),PIC+(POS + 1)+选择,Toast.LENGTH_SHORT).show();
    }
    });
}
公共类ImageAdapter延伸BaseAdapter
{
    私人上下文的背景下;
    私人诠释itemBackground;
    ImageAdapter(上下文C)
    {
    上下文= C;
    TypedArray A = obtainStyledAttributes(R.styleable.Gallery1);
    itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
    a.recycle();
    }
    公众诠释getCount将()
    {
        返回urls.length;
    }
    公共对象的getItem(INT POS)
    {
        返回POS;
    }
    众长getItemId(INT POS)
    {
        返回POS;
    }
    公共查看getView(INT POS,查看简历的ViewGroup以及VG)
    {
位图位图= DownloadImage(网址[POS]);
ImageView的ImageView的=新ImageView的(上下文);
imageview.setImageBitmap(位图);
返回简历;
    }
私人位图DownloadImage(字符串URL)
    {
        最后弦乐URL1 =网址;
        新的Thread()
        {
            公共无效的run()
            {
                在的InputStream = NULL;
                消息味精= Message.obtain();
                msg.what = 1;
                尝试{
                    在= OpenHttpConnection(URL1);
                    位图位图= BitmapFactory.de codeStream(中);
                    束B =新包();
                    b.putParcelable(位图,位图);
                    msg.setData(二);
                    附寄();
                }赶上(IOException异常E1){
                    // TODO自动生成catch块
                    e1.printStackTrace();
                }
            }
        }。开始();
        返回位图;
    }
    私人的InputStream OpenHttpConnection(字符串urlString)
            抛出IOException异常
            {
                在的InputStream = NULL;
                INT响应= -1;
                网址URL =新的URL(urlString);
                康涅狄格州的URLConnection = url.openConnection();
                如果(!(康涅狄格州的instanceof HttpURLConnection类))
                    抛出新IOException异常(不是一个HTTP连接);
                尝试{
                    HttpURLConnection的httpConn =(HttpURLConnection类)美国康涅狄格州;
                    httpConn.setAllowUserInteraction(假);
                    httpConn.setInstanceFollowRedirects(真);
                    httpConn.setRequestMethod(GET);
                    httpConn.connect();
                    响应= httpConn.getResponse code();
                    如果(响应== HttpURLConnection.HTTP_OK)
                    {
                        在= httpConn.getInputStream();
                    }
                }
                赶上(异常前)
                {
                    抛出新IOException异常(错误连接);
                }
                返回;
    }
}
}


logcat的


  E / AndroidRuntime(832):显示java.lang.NullPointerException
       E / AndroidRuntime(832):在android.widget.GridView.onMeasure(GridView.java:937)
        E / AndroidRuntime(832):在android.view.View.measure(View.java:8313)
       E / AndroidRuntime(832):在
       android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       E / AndroidRuntime(832):在
       android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
        E / AndroidRuntime(832):在
        android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
         E / AndroidRuntime(832):在
        android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
         E / AndroidRuntime(832):在android.view.View.measure(View.java:8313)
         E / AndroidRuntime(832):在
         android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
          E / AndroidRuntime(832):在android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
         E / AndroidRuntime(832):在android.view.View.measure(View.java:8313)
         E / AndroidRuntime(832):在
          android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
          E / AndroidRuntime(832):在
          android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
          E / AndroidRuntime(832):在android.view.View.measure(View.java:8313)
          E / AndroidRuntime(832):在
          android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
          E / AndroidRuntime(832):在android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
          E / AndroidRuntime(832):在android.view.View.measure(View.java:8313)
          E / AndroidRuntime(832):在android.view.ViewRoot.performTraversals(ViewRoot.java:839)
          E / AndroidRuntime(832):在android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
          E / AndroidRuntime(832):在android.os.Handler.dispatchMessage(Handler.java:99)
           E / AndroidRuntime(832):在android.os.Looper.loop(Looper.java:123)
           E / AndroidRuntime(832):在android.app.ActivityThread.main(ActivityThread.java:3683)
           E / AndroidRuntime(832):在java.lang.reflect.Method.invokeNative(本机方法)
           E / AndroidRuntime(832):在java.lang.reflect.Method.invoke(Method.java:507)
           E / AndroidRuntime(832):在
           com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
            E / AndroidRuntime(832):在
           com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            E / AndroidRuntime(832):在dalvik.system.NativeStart.main(本机方法)



解决方案

这是codeI试图..Images是displaying..You自定义网格视图作为您need.You只需复制并粘贴下面的code在您的活动,并尝试..

 公共类MainActivity延伸活动{    静态字符串uri1 =htt​​p://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg;
    静态字符串uri2 =htt​​p://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg;
    静态字符串uri3 =htt​​p://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg;
    公共静态的String [] =网址{uri1,uri2,uri3};
//公共位图位图;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        GridView控件GRD =(GridView控件)findViewById(R.id.gridview1);
        grd.setAdapter(新ImageAdapter(本));
    }
    公共类ImageAdapter延伸BaseAdapter
    {
        私人上下文的背景下;
        私人诠释itemBackground;
        ImageAdapter(上下文C)
        {
        上下文= C;
        }
        公众诠释getCount将()
        {
            返回urls.length;
        }
        公共对象的getItem(INT POS)
        {
            返回POS;
        }
        众长getItemId(INT POS)
        {
            返回POS;
        }    私人位图DownloadImage(字符串URL)
        {
             字符串URL1 =网址;
             位图位图= NULL;
//新的Thread()
// {
//公共无效的run()
// {
                    在的InputStream = NULL;
                    消息味精= Message.obtain();
                    msg.what = 1;
                    尝试{
                        在= OpenHttpConnection(URL1);
                        位= BitmapFactory.de codeStream(中);
                        束B =新包();
                        b.putParcelable(位图,位图);
                        msg.setData(二);
                        附寄();
                    }赶上(IOException异常E1){
                        // TODO自动生成catch块
                        e1.printStackTrace();
                    }
//}
//}。开始();
            返回位图;
        }
        私人的InputStream OpenHttpConnection(字符串urlString)
                抛出IOException异常
                {
//的System.out.println(Insdie康恩);
                    在的InputStream = NULL;
                    INT响应= -1;
                    网址URL =新的URL(urlString);
                    康涅狄格州的URLConnection = url.openConnection();
                    如果(!(康涅狄格州的instanceof HttpURLConnection类))
                        抛出新IOException异常(不是一个HTTP连接);
                    尝试{
//的System.out.println(内部试);
                        HttpURLConnection的httpConn =(HttpURLConnection类)美国康涅狄格州;
                        httpConn.setAllowUserInteraction(假);
                        httpConn.setInstanceFollowRedirects(真);
// httpConn.setRequestMethod(GET);
                        httpConn.connect();
                        响应= httpConn.getResponse code();
//的System.out.println(RES =+响应);
//的System.out.println(CCCCCC =+ HttpURLConnection.HTTP_OK);
                        如果(响应== HttpURLConnection.HTTP_OK)
                        {
//的System.out.println(内部如果);
                            在= httpConn.getInputStream();
                        }
                    }
                    赶上(异常前)
                    {
                        抛出新IOException异常(错误连接);
                    }
                    返回;
        }
        @覆盖
        公共查看getView(INT位置,查看简历的ViewGroup以及母公司)
        {
            ImageView的ImageView的= NULL;
//的System.out.println(vvvv的=+网址[位置]);
            位图位图= DownloadImage(网址[位置]);
            // TODO自动生成方法存根
            如果(CV == NULL)
            {
// CV = LayoutInflater.from(parent.getContext())膨胀(R.layout.gridviewitem,NULL);
                ImageView的=新ImageView的(上下文);
            }
            其他
            {
                ImageView的=(ImageView的)简历;
            }            imageview.setImageBitmap(位图);
            返回ImageView的;
        }
    }

I have set a gridview for image url..I can't able to see the image in grid.. Just shows the background...and on click the grid plays in next screen.

What im doing wrong? How to implement this?

Thanks a lot in advance

my code

    public class act extends Activity {
static  String uri1="https://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg";
static String uri2="https://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg";
static String uri3="https://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg";
public static String[] urls={uri1,uri2,uri3};
public Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    GridView grd=(GridView)findViewById(R.id.gridView1);
    grd.setAdapter(new ImageAdapter(this));
    grd.setOnItemClickListener(new OnItemClickListener()
    {
    public void onItemClick(AdapterView<?> parent,View v,int pos,long id)
    {
        Toast.makeText(getBaseContext(),"pic"+(pos+1)+"select ",Toast.LENGTH_SHORT).show();
    }
    });
}
public class ImageAdapter extends BaseAdapter
{
    private Context context;
    private int itemBackground;
    ImageAdapter(Context c)
    {
    context=c;
    TypedArray a=obtainStyledAttributes(R.styleable.Gallery1);
    itemBackground=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
    a.recycle();
    }
    public int getCount()
    {
        return urls.length;
    }
    public Object getItem(int pos)
    {
        return pos;
    }
    public long getItemId(int pos)
    {
        return pos;
    }
    public View getView(int pos,View cv,ViewGroup vg)
    {
Bitmap bitmap=  DownloadImage( urls[pos] );
ImageView imageview=new ImageView(context);
imageview.setImageBitmap(bitmap);
return cv;    
    }
private Bitmap DownloadImage(String URL)
    {        
        final String URL1=URL;       
        new Thread()
        {
            public void run()
            {               
                InputStream in = null;  
                Message msg = Message.obtain();
                msg.what = 1;
                try {
                    in = OpenHttpConnection(URL1);
                    Bitmap bitmap = BitmapFactory.decodeStream(in);     
                    Bundle b = new Bundle();
                    b.putParcelable("bitmap", bitmap);
                    msg.setData(b);
                    in.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }.start();
        return bitmap;
    }
    private InputStream OpenHttpConnection(String urlString)
            throws IOException
            {
                InputStream in = null;
                int response = -1;
                URL url = new URL(urlString);
                URLConnection conn = url.openConnection();
                if (!(conn instanceof HttpURLConnection))                    
                    throw new IOException("Not an HTTP connection");
                try{
                    HttpURLConnection httpConn = (HttpURLConnection) conn;
                    httpConn.setAllowUserInteraction(false);
                    httpConn.setInstanceFollowRedirects(true);
                    httpConn.setRequestMethod("GET");
                    httpConn.connect();
                    response = httpConn.getResponseCode();                
                    if (response == HttpURLConnection.HTTP_OK) 
                    {
                        in = httpConn.getInputStream();                                
                    }                    
                }
                catch (Exception ex)
                {
                    throw new IOException("Error connecting");            
                }
                return in;    
    }
}
}

logcat

     E/AndroidRuntime(832): java.lang.NullPointerException
       E/AndroidRuntime(832):   at android.widget.GridView.onMeasure(GridView.java:937)
        E/AndroidRuntime(832):  at android.view.View.measure(View.java:8313)
       E/AndroidRuntime(832):   at 
       android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
       E/AndroidRuntime(832):   at 
       android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
        E/AndroidRuntime(832):  at 
        android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
         E/AndroidRuntime(832):     at 
        android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
         E/AndroidRuntime(832):     at android.view.View.measure(View.java:8313)
         E/AndroidRuntime(832):     at 
         android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
          E/AndroidRuntime(832):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
         E/AndroidRuntime(832):     at android.view.View.measure(View.java:8313)
         E/AndroidRuntime(832):     at 
          android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
          E/AndroidRuntime(832):    at 
          android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
          E/AndroidRuntime(832):    at android.view.View.measure(View.java:8313)
          E/AndroidRuntime(832):    at 
          android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
          E/AndroidRuntime(832):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
          E/AndroidRuntime(832):    at android.view.View.measure(View.java:8313)
          E/AndroidRuntime(832):    at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
          E/AndroidRuntime(832):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
          E/AndroidRuntime(832):    at android.os.Handler.dispatchMessage(Handler.java:99)
           E/AndroidRuntime(832):   at android.os.Looper.loop(Looper.java:123)
           E/AndroidRuntime(832):   at android.app.ActivityThread.main(ActivityThread.java:3683)
           E/AndroidRuntime(832):   at java.lang.reflect.Method.invokeNative(Native Method)
           E/AndroidRuntime(832):   at java.lang.reflect.Method.invoke(Method.java:507)
           E/AndroidRuntime(832):   at 
           com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            E/AndroidRuntime(832):  at 
           com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            E/AndroidRuntime(832):  at dalvik.system.NativeStart.main(Native Method)

解决方案

This is the code i tried ..Images are displaying..You customize the grid view as your need.You just copy and paste the below code in your activity and try..

public class MainActivity extends Activity {

    static  String uri1="http://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg";
    static String uri2="http://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg";
    static String uri3="http://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg";
    public static String[] urls={uri1,uri2,uri3};
//  public Bitmap bitmap;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView grd=(GridView)findViewById(R.id.gridview1);
        grd.setAdapter(new ImageAdapter(this));


    }
    public class ImageAdapter extends BaseAdapter
    {
        private Context context;
        private int itemBackground;
        ImageAdapter(Context c)
        {
        context=c;    
        }
        public int getCount()
        {
            return urls.length;
        }
        public Object getItem(int pos)
        {
            return pos;
        }
        public long getItemId(int pos)
        {
            return pos;
        }

    private Bitmap DownloadImage(String URL)
        {        
             String URL1=URL; 
             Bitmap bitmap = null;
//          new Thread()
//          {
//              public void run()
//              {               
                    InputStream in = null;  
                    Message msg = Message.obtain();
                    msg.what = 1;
                    try {
                        in = OpenHttpConnection(URL1);
                        bitmap = BitmapFactory.decodeStream(in);     
                        Bundle b = new Bundle();
                        b.putParcelable("bitmap", bitmap);
                        msg.setData(b);
                        in.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
//              }
//          }.start();
            return bitmap;
        }
        private InputStream OpenHttpConnection(String urlString)
                throws IOException
                {
//          System.out.println("Insdie conn");
                    InputStream in = null;
                    int response = -1;
                    URL url = new URL(urlString);
                    URLConnection conn = url.openConnection();
                    if (!(conn instanceof HttpURLConnection))                    
                        throw new IOException("Not an HTTP connection");
                    try{
//                      System.out.println("Inside try");
                        HttpURLConnection httpConn = (HttpURLConnection) conn;
                        httpConn.setAllowUserInteraction(false);
                        httpConn.setInstanceFollowRedirects(true);
//                      httpConn.setRequestMethod("GET");
                        httpConn.connect();
                        response = httpConn.getResponseCode();  
//                      System.out.println("res="+response);
//                      System.out.println("cccccc="+HttpURLConnection.HTTP_OK);
                        if (response == HttpURLConnection.HTTP_OK) 
                        {
//                          System.out.println("Inside if");
                            in = httpConn.getInputStream();                                
                        }                    
                    }
                    catch (Exception ex)
                    {
                        throw new IOException("Error connecting");            
                    }
                    return in;    
        }
        @Override
        public View getView(int position, View cv, ViewGroup parent) 
        {
            ImageView imageview = null;
//          System.out.println("vvvv="+urls[position]);
            Bitmap bitmap=  DownloadImage( urls[position] );        
            // TODO Auto-generated method stub
            if(cv == null)
            {
//              cv=LayoutInflater.from(parent.getContext()).inflate(R.layout.gridviewitem, null);
                imageview =  new ImageView(context);
            }
            else 
            {
                imageview = (ImageView) cv;
            }

            imageview.setImageBitmap(bitmap);
            return imageview;    
        }
    }

这篇关于图像URL未在网格中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆