简体   繁体   中英

how to implement a viewpager to load images from server side?

I have written a server side program in jsp to insert images into my database and its working fine. Now i want to load the images from the URL inside a ViewPager (client side) I have written a CustomPagerAdapter class and have an activity in which i want to call the pager adapter class, i am getting an error Cannot decode stream FileNotFoundException please help me to figure out how to solve the problem and how to implement a ViewPager .

CustomPagerAdapter.java

public class CustomPagerAdapter extends PagerAdapter {

Context mContext;
LayoutInflater mLayoutInflater;
ArrayList<String> imgUrl = new ArrayList<String>();
Bitmap bitmap;

public CustomPagerAdapter(Context context, ArrayList<String> imageURL) {
    mContext = context;

    this.imgUrl = imageURL;
}

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

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

public Object instantiateItem(ViewGroup container, int position) {
    mLayoutInflater = (LayoutInflater) 
    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, 
    false);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    bitmap = BitmapFactory.decodeFile(imgUrl.get(position), options);
    TouchImageView imgview = (TouchImageView) 
    itemView.findViewById(R.id.imgEvent);
    imgview.setImageBitmap(bitmap);

    container.addView(itemView);

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((RelativeLayout) object);
}

EventType_Images.java : Here i have converted the String imgUrl to arraylist , is it right ? if not please tell me the correct way

public class EventType_Images extends Activity {

TextView txtHeader;
String passedData, passedData1;
public static String imgURL;
String images;
ViewPager viewPager;
ArrayList<String> imgList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.eventtype_images);
    passedData = getIntent().getStringExtra("name");
    txtHeader = (TextView) findViewById(R.id.txtHeader);
    passedData1 = getIntent().getStringExtra("type");
    txtHeader.setText(passedData1);
    images = getIntent().getStringExtra("imageName");
    viewPager = (ViewPager) findViewById(R.id.pager);

    imgURL = "http://197.188.1.50:8082/Server/Gallery/" + images;
    imgList = new ArrayList<String>(Arrays.asList(imgURL));

    new MyAsyncTask(this,  
    imgList).execute("http://197.188.1.50:8082/Server/Gallery/"+ images);

}
}

class MyAsyncTask extends AsyncTask<String, Integer, Object> {

private Context context;
private ViewPager viewPager;
CustomPagerAdapter adapter;

public MyAsyncTask(Context context, ArrayList<String> imgList) {
    this.context = context;
    this.viewPager = viewPager;
    this.imgList = imgList;
}

protected Object doInBackground(String... params) {
    URL imgUrl = null;
    InputStream is = null;
    HttpURLConnection conn = null;
    try {
        imgUrl = new URL(EventType_Images.imgURL);
        conn = (HttpURLConnection) imgUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();
        bm = BitmapFactory.decodeStream(is);// Convert to bitmap
    } catch (Exception e) {
        e.printStackTrace();
    }

    return adapter;
}

@Override
protected void onPostExecute(Object result) {
    dialog.dismiss();
    // imgview.setImageBitmap(bm);
    adapter = new CustomPagerAdapter(context,imgList);
    viewPager.setAdapter(adapter);
    super.onPostExecute(result);
}

Error

02-24 17:00:57.492: E/BitmapFactory(12895): Unable to decode stream: java.io.FileNotFoundException: /http:/197.188.1.50:8082/Server/Gallery/1.jpg2.jpg: open failed: ENOENT (No such file or directory) the image names are also coming one after the other

Thanking You

Assuming your log entry was accurately listed here, the URL seems wrong:

java.io.FileNotFoundException: 
/http:/197.188.1.50:8082/Server/Gallery/[1.jpg2.jpg

This does not look like a proper URL.

Also, you asked if it's correct to convert the string to an ArrayList . This code is very strange. You are keeping a list as a static member in your activity, and it's being referenced by your AsyncTask , instead of just passing it along to your AsyncTask . doInBackground has support for parameters passed into the task - use it!

You should typically never expose any data from an activity as a static member as it does not respect activity life cycle etc.

I would start by verifying that you are passing the right URL. Fix your AsyncTask so that you are passing along the parameters you need, remove your statically exposed members, and add a log entry in doInBackground that prints the URL you are trying to connect to. As we have no idea what your "images" parameter in the intent is holding, it's hard to say what you are really passing into that list - but in any case it will always be a list of ONE element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM