如何从服务器检索多个图像并逐个显示

时间:2013-04-16 09:27:51

标签: android

在我的应用中,我需要从服务器流式传输视频和图像。如果视频不可用,则应检索图像并将其显示在照片库中。我成功地做了这个部分,但一次只有一个图像。但是这种方法很慢,因为它需要很长时间来加载下一张图像。

我想要做的是传递4个网址(4个是此示例中要从服务器检索的图像总数),但是通过使用Onclicklistener逐个显示在我的imageview中。  请给我任何建议。我该怎么办?感谢

public class Imageviewing extends Activity implements  OnGestureListener{

    private static final String TAG = null;
    ImageView imView;
    String imageUrl="<myServerlink>";
    Random r= new Random();
    String urilink;
    private GestureDetector myGesture ;
    private static final int SWIPE_MIN_DISTANCE = 75;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageviewing);

        Intent intent = getIntent();
        // setContentView(R.layout.);
        // String

        urilink = intent.getExtras().getString("videouri");
        Log.e(TAG, urilink.toString());
        Button bt3= (Button)findViewById(R.id.get_imagebt);
        bt3.setOnClickListener(getImgListener);
        imView = (ImageView)findViewById(R.id.imview);
        myGesture = new GestureDetector(this);

    }
    View.OnClickListener getImgListener = new View.OnClickListener()
    {

          @Override
          public void onClick(View view) {

              finish();
          }

    };

    @Override
    public boolean onTouchEvent(MotionEvent event){
        return myGesture.onTouchEvent(event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_imageviewing, menu);
        return true;
    }

     Bitmap bmImg;
        void downloadFile(String fileUrl){
              URL myFileUrl =null;          
              try {
                   myFileUrl= new URL(fileUrl);
              } catch (MalformedURLException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              }
              try {
                   HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
                   conn.setDoInput(true);
                   conn.connect();
                   InputStream is = conn.getInputStream();

                   bmImg = BitmapFactory.decodeStream(is);
                   imView.setImageBitmap(bmImg);
                   Log.i("im url","Image to Imageview");
              } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              }
         }

        @Override
        public boolean onDown(MotionEvent arg0) {
            // TODO Auto-generated method stub
            return false;
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            // TODO Auto-generated method stub
            try {
                //do not do anything if the swipe does not reach a certain length of distance
               // if (Math.abs(e1.getY() - e2.getY()) &gt; SWIPE_MAX_OFF_PATH)
                   // return false;
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;

                // right to left swipe
                //if(e1.getX() - e2.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) {

                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    int i = r.nextInt(5-1)+1;
                    downloadFile(imageUrl+urilink+"-"+i+".jpg");
                    Log.i("im url",imageUrl+urilink+"-"+i+".jpg");
                    return false;

                }
               /* // left to right swipe
                else if (e2.getX() - e1.getX() &gt; SWIPE_MIN_DISTANCE &amp;&amp; Math.abs(velocityX) &gt; SWIPE_THRESHOLD_VELOCITY) {
                    ViewHistory();
                }*/
            } catch (Exception e) {
                // nothing
            }
            return false;

        }
        @Override
        public void onLongPress(MotionEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
                float arg3) {
            // TODO Auto-generated method stub
            return false;
        }
        @Override
        public void onShowPress(MotionEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public boolean onSingleTapUp(MotionEvent arg0) {
            // TODO Auto-generated method stub
            return false;
        }

}

1 个答案:

答案 0 :(得分:0)

有关如何使用所谓的延迟加载有效加载多个图像的示例,请参阅此问题。

Lazy load of images in ListView