Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 包含来自web的图像的异步任务库_Android - Fatal编程技术网

Android 包含来自web的图像的异步任务库

Android 包含来自web的图像的异步任务库,android,Android,我有一个问题我不能解决qal。 我已经尝试了所有的方法。 这是AsyncTask中的一个问题,它不应该是正确的 有没有人能用正确的方式介绍我 public class ExibeGallery extends SherlockActivity { private Gallery galleryView1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreat

我有一个问题我不能解决qal。 我已经尝试了所有的方法。 这是AsyncTask中的一个问题,它不应该是正确的

有没有人能用正确的方式介绍我

public class ExibeGallery extends SherlockActivity {
    private Gallery galleryView1;
    @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exibegallery);
        Log.i("19", "Chama a gallery");         
        AdaptadordaImagem adp =  new AdaptadordaImagem(this);
        galleryView1 = (Gallery) findViewById(R.id.GaleryView);
        galleryView1.setAdapter(adp);
    } 
}

public class AdaptadordaImagem extends BaseAdapter {
    private Context cont;
    int GalItemBg;
    private String[] myRemoteImages = {
        "http://www.cuboweb.com.br/android/images/1.png",
        "http://www.cuboweb.com.br/android/images/2.png",
        "http://www.cuboweb.com.br/android/images/3.png",
        "http://www.cuboweb.com.br/android/images/4.png", };
        static Bitmap bm;
        private int[] img = {0, 1, 2, 3};
        BitmapFactory.Options bmOptions;
        private Object mGalleryItemBackground;
        ImageView i = null;

    public AdaptadordaImagem(Context c) {
    cont = c;

    i = new ImageView(this.cont);
    PegaImagens pg = new PegaImagens();
    pg.execute();
    Log.i("1", "Adaptador de imagem");  
   }

        private TypedArray obtainStyledAttributes(int[] hellogallery) {
            // TODO Auto-generated method stub
            return null;
        }

        public int getCount() {
            return this.img.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            Log.i("2", "getView");
            ImageView i = new ImageView(cont);
            i.setBackgroundResource(img[position]);
            i.setLayoutParams(new Gallery.LayoutParams(150, 100));
            i.setBackgroundResource((Integer) mGalleryItemBackground);
            i.setAdjustViewBounds(true);
            return i;   
        }

        public class PegaImagens extends AsyncTask<String[], Void, String> {
            private ProgressDialog progressgallery;

            protected void onPreExecute() {
                Log.i("3", "AsyncTask onPreexecute");
                progressgallery = new ProgressDialog(cont, GalItemBg);
                progressgallery.setTitle("Aguarde...");
                progressgallery.setMessage("Carregando...");
                progressgallery.show();
            }

            public Bitmap loadBitmap(String[] myRemoteImages, BitmapFactory.Options options) {
                Log.i("4", "AsyncTask loadBitmap");
                InputStream in = null;
                int i;
                for (i = 0; i <= 3; i++) {
                    Log.i("5", "AsyncTask loadBitmap for");
                    try {
                        Log.i("5", "AsyncTask loadBitmap for try");
                        in = OpenHttpConnection(myRemoteImages[i]);
                        bm = BitmapFactory.decodeStream(in, null, options);
                        in.close();
                    } catch (IOException e1) {
                        Log.i("6", "AsyncTask loadBitmap for catch");
                        bm = null;
                    }
                }
                return bm;
            }// aqui fecha o load bitmap

            private InputStream OpenHttpConnection(String myRemoteImages) throws IOException {
                Log.i("6", "AsyncTask OpenHttpConnection");
                InputStream inputStream = null;
                URL url = new URL(myRemoteImages);
                URLConnection conn = url.openConnection();
                try {
                    Log.i("7", "AsyncTask OpenHttpConnection try");
                    HttpURLConnection httpConn = (HttpURLConnection) conn;
                    httpConn.setRequestMethod("GET");
                    httpConn.connect();
                    InputStream is = conn.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    Bitmap bm = BitmapFactory.decodeStream(bis);
                    bis.close();
                    is.close();
                    i.setImageBitmap(bm);
                    if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        Log.i("8", "AsyncTask OpenHttpConnection try if");
                        inputStream = httpConn.getInputStream();
                    }
                } catch (Exception ex) {
                    Log.i("9", "AsyncTask OpenHttpConnection catch");
                    i.setImageResource(R.drawable.sem_conexao);
                }
                return inputStream;
            }// aqui fecha o InputStream

            protected String doInBackground(String[]... params) {
                // TODO Auto-generated method stub
                Log.i("10", "AsyncTask doInBackground");
                int x;
                for (x = 0; x <= 3; x++) {
                    Log.i("11", "AsyncTask doInBackground for");
                    try {
                        Log.i("12", "AsyncTask doInBackground for try");
                        bmOptions = new BitmapFactory.Options();
                        bmOptions.inSampleSize = 1;
                    } catch (ArrayIndexOutOfBoundsException erro) {
                        Log.i("13", "AsyncTask doInBackground for try catch1");
                        myRemoteImages[x] = "";
                    } catch (Exception erro) {
                        Log.i("14", "AsyncTask doInBackground for try catch2");
                        myRemoteImages[x] = "";
                    }
                }
                loadBitmap(myRemoteImages, bmOptions);
                return myRemoteImages[x];
            }

            protected void onPostExecute(String[] myRemoteImages) {
                Log.i("15", "AsyncTask onPostExecute ");
                if (!myRemoteImages.equals("")) {
                    // ok
                    Log.i("16", "AsyncTask onPostExecute if ok ");
                    TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
                    mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
                    a.recycle();
                } else {
                    Log.i("17", "AsyncTask onPostExecute else falha ");
                }
                progressgallery.dismiss();
            }
        }// end async task
    }// end class

故障如下所示:

ImageView i = new ImageView(this.cont);
将此行添加到构造函数,如下所示:

//this line edited;
ImageView i = null;

public AdaptadordaImagem(Context c) {
    cont = c;

    //this line added
    i = new ImageView(this.cont);

    PegaImagens pg = new PegaImagens();
    pg.execute();
    Log.i("1", "Adaptador de imagem");
}
或者甚至删除它,因为它似乎没有在代码中真正使用(在getView方法上创建新的ImageView i)


希望能有帮助。Saludos

看起来您的img从未初始化过,至少在发布的代码中是这样。如果是这样,getCount()将正确地获取null

如果这是真的,您将需要以下内容:

private int[] img = someIntArrayWithYourIds;
编辑:澄清:

尝试在getCount的此行中设置调试器断点,并查看img和img.length的值:

        return this.img.length;

很可能,img在那里是空的。

@Flip\u novidade您在清单中声明了您的活动了吗?是的。在这里,你可以尝试完美的教程,那里有更多的法令图像质量。我理解,大多数仍然有问题,我仍然有问题。我没有。我可以演示。我的问题在InputStream OpenHttpConnection中。我仍然有问题。。。有人可以帮我理解。我没有。我可以演示。编辑答案:看起来img没有初始化,因此是空的。我仍然有问题。。。有人可以帮我
        return this.img.length;