2015-08-14 3 views
0

Моего кода выглядит следующим образом:Как показать мои все изображения в представлении изображения в андроиде

public class MainActivity extends Activity { 
    ImageView[] targetImage = new ImageView[5]; 
    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) { 
      Log.d("Networking", ex.getLocalizedMessage()); 
      throw new IOException("Error connecting"); 
     } 

     return in; 
    } 

    private Bitmap DownloadImage(String URL) { 
     Bitmap bitmap = null; 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      bitmap = BitmapFactory.decodeStream(in); 
      in.close(); 
     } catch (IOException e1) { 
      Log.d("NetworkingActivity", e1.getLocalizedMessage()); 
     } 

     return bitmap; 
    } 

    private class DownloadImageTask extends AsyncTask<String, Bitmap, Long> { 
     //---takes in a list of image URLs in String type--- 
     protected Long doInBackground(String... urls) { 
      long imagesCount = 0; 
      for (int i = 0; i < urls.length; i++) { 
       //---download the image--- 
       Bitmap imageDownloaded = DownloadImage(urls[i]); 
       if (imageDownloaded != null) { 
        //---increment the image count--- 
        imagesCount++; 
        try { 
         //---insert a delay of 3 seconds--- 
         Thread.sleep(3000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 

        //---return the image downloaded--- 
        publishProgress(imageDownloaded); 
       } 
      } 

      //---return the total images downloaded count--- 
      return imagesCount; 
     } 

     //---display the image downloaded--- 
     protected void onProgressUpdate(Bitmap... bitmap) { 
      if(bitmap.length > 0) { 
       for(int i = 0; i < bitmap.length; i++) { 
        targetImage[i].setImageBitmap(bitmap[i]); 
       } 
      } 
      // tIV[values[i]].setImageBitmap(tBM[values[i]]); 
     } 

     //---when all the images have been downloaded--- 
     protected void onPostExecute(Long imagesDownloaded) { 
      Toast.makeText(getBaseContext(), 
      "Total" + imagesDownloaded + " images downloaded" , 
      Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     targetImage[0] = (ImageView)findViewById(R.id.target0); 
     targetImage[1] = (ImageView)findViewById(R.id.target1); 
     targetImage[2] = (ImageView)findViewById(R.id.target2); 

     new DownloadImageTask().execute(
      "http://solutionboat.com/work_2/asset/images/sobin.jpg" , 
      "http://solutionboat.com/work_2/asset/images/shamol.jpg", 
      "http://solutionboat.com/work_2/asset/images/rifat.jpg" 
     ); 

     ViewFlipper flipper = (ViewFlipper) findViewById(R.id.viewFlipper1); 
     flipper.startFlipping(); 
    } 
} 

ответ

0

Импорта библиотека Picasso в проекте:

http://square.github.io/picasso/

И попробуйте это:

public class MainActivity extends Activity { 
      ImageView[] targetImage = new ImageView[5]; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        targetImage[0] = (ImageView)findViewById(R.id.target0); 
        targetImage[1] = (ImageView)findViewById(R.id.target1); 
        targetImage[2] = (ImageView)findViewById(R.id.target2); 
Picasso.with(MainActivity.this).load("http://solutionboat.com/work_2/asset/images/sobin.jpg").into(targetImage[0]); 
     Picasso.with(MainActivity.this).load("http://solutionboat.com/work_2/asset/images/shamol.jpg").into(targetImage[1]); 
     Picasso.with(MainActivity.this).load("http://solutionboat.com/work_2/asset/images/rifat.jpg").into(targetImage[2]); 
    } 
    } 
Смежные вопросы