Android中的缩略图视图

时间:2010-08-20 12:49:31

标签: android

我正在开发一个Android应用程序,它基本上访问给定的URL并下载图像。

现在我想填充这个下载的图像并以缩略图视图显示它。

我已尝试从SD卡中显示相同内容,但是当我为下载的文件执行相同操作时,它似乎无法正常工作。

public View getView(int position,View convertView,ViewGroup parent)                
{  
  System.gc();  
  ImageView i = new ImageView(mContext.getApplicationContext());  
    if (convertView == null)    
    {  
          //imagecursor.moveToPosition(position);  
          //int id = imagecursor.getInt(image_column_index);  
          //i.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+ id));  
          i.setImageBitmap(bitmap);  
          i.setScaleType(ImageView.ScaleType.FIT_CENTER);  
          i.setLayoutParams(new GridView.LayoutParams(100, 100));  
    }  
    else  
    {  
       i = (ImageView) convertView;
    }
    return i;  
}

1 个答案:

答案 0 :(得分:1)

您可以下载该文件并使用URL,HttpConnection& amp;的InputStream / Outputsteam。下载后,您可以在ImageView中显示它。

我使用AsyncTask完成了这个:

@Override
        protected Void doInBackground(URL... urls) {
            for (int i = 0; i < urls.length; i++) {

                try {

                    Pattern pattern = Pattern.compile("([a-zA-Z]*.[jpeg|png])$");
                    Matcher matcher = pattern.matcher(urls[i].getFile());

                    if (matcher.find()) {

                        URLConnection connection = urls[i].openConnection();
                        int fileSize = connection.getContentLength();
                        mFileName = matcher.group();

                        String filePath = Environment
                                .getExternalStorageDirectory()
                                + File.separator + mFileName;

                        InputStream input = new BufferedInputStream(urls[i]
                                .openStream());

                        OutputStream output = new FileOutputStream(filePath);

                        byte data[] = new byte[1024];

                        long total = 0;
                        int count;

                        while ((count = input.read(data)) != -1) {
                            total += count;
                            int value = (int) (total * 100 / fileSize);
                            output.write(data, 0, count);
                            onProgressUpdate(value);
                        }

                        output.flush();
                        output.close();
                        input.close();

                        // Now display the image in your ImageView

                    }

                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
            return null;
        }