我使用下面的代码简单地显示链接中的多个图像但应用程序已关闭

时间:2014-09-08 04:09:02

标签: android android-layout android-activity android-fragments android-gridview

我在网站上显示的图像上工作得像这样,我的代码是:

LetsTry.java

public class LetsTry extends PagerAdapter {

    Context context;

    String[] imageLocation = { "http://sandipjagdale.com/images/logo.png",
            "http://sandipjagdale.com/images/logo.png",
            "http://sandipjagdale.com/images/logo.png" };

    public LetsTry(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imageLocation.length;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == ((ImageView)arg1);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){

        ImageView iv = new ImageView(context);
        iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        for(int i = 0; i < imageLocation.length; i++)
        {
            iv.setImageResource(imageLocation[i]);
        }

        return iv;

    }
}

Main.Java

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        LetsTry adapter = new LetsTry(this);
        viewPager.setAdapter(adapter);
    }

}

iv.setImageResource下显示错误 我按照manishkpr中的代码进行操作。如何使用此网址网址图片显示此类图片库。

2 个答案:

答案 0 :(得分:0)

ls使用此代码将您的图像Url设置为图像视图。

  for(int i = 0; i < imageLocation.length; i++)
    {
        iv.setImageBitmap(getBitmapFromURL(imageLocation[i]));
    }



 public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
} 

演示:

   public class MainActivity extends ActionBarActivity {

ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    StrictMode.ThreadPolicy policy = 
            new StrictMode.ThreadPolicy.Builder().permitAll().build();      
                StrictMode.setThreadPolicy(policy);

    iv=(ImageView)findViewById(R.id.imageView1);

    iv.setImageBitmap(getBitmapFromURL("http://sandipjagdale.com/images/logo.png"));
}

public static Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
} 

}

工作正常。

  public class GalleryView extends Activity {

String[] imageLocation = { "http://sandipjagdale.com/images/logo.png",
        "http://sandipjagdale.com/images/logo.png",
        "http://sandipjagdale.com/images/logo.png" };
LinearLayout imageView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    StrictMode.ThreadPolicy policy = 
            new StrictMode.ThreadPolicy.Builder().permitAll().build();      
                StrictMode.setThreadPolicy(policy);


    try {
        // InputStream in = (new URL("www.google.com").openStream());
    } catch (Exception e) {
        e.getMessage();
    }
    Gallery ga = (Gallery) findViewById(R.id.Gallery01);
    ga.setAdapter(new ImageAdapter(this));


}

public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {

        return imageLocation.length;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);

        iv.setImageBitmap(getBitmapFromURL(imageLocation[arg0]));
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }
}

 public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    } 

}

答案 1 :(得分:0)

尝试使用Universal Image Loader lib for android。以下是解释如何使用它的链接。

http://javatechig.com/android/universal-image-loader-android-library