Java 如何实现viewpager从服务器端加载图像?

Java 如何实现viewpager从服务器端加载图像?,java,android,Java,Android,我已经用jsp编写了一个服务器端程序,将图像插入到我的数据库中,并且运行良好。 现在我想从ViewPager(客户端)中的URL加载图像 我已经编写了一个CustomPagerAdapter类,并且有一个活动,我想在其中调用寻呼机适配器类,我收到一个错误无法解码流文件NotFoundException 请帮助我了解如何解决此问题以及如何实现ViewPager CustomPagerAdapter.java public class CustomPagerAdapter extends Pager

我已经用jsp编写了一个服务器端程序,将图像插入到我的数据库中,并且运行良好。 现在我想从
ViewPager
(客户端)中的URL加载图像 我已经编写了一个
CustomPagerAdapter
类,并且有一个活动,我想在其中调用寻呼机适配器类,我收到一个错误无法解码流文件NotFoundException 请帮助我了解如何解决此问题以及如何实现
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);
}
public类CustomPagerAdapter扩展了PagerAdapter{
语境;
放平机MLAYOUTIONFLATER;
ArrayList imgUrl=新的ArrayList();
位图;
公共CustomPagerAdapter(上下文,ArrayList imageURL){
mContext=上下文;
this.imgUrl=imageURL;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回imgUrl.size();
}
@凌驾
公共布尔值isViewFromObject(视图arg0,对象arg1){
//TODO自动生成的方法存根
返回arg0==((RelativeLayout)arg1);
}
公共对象实例化项(视图组容器,int位置){
MLAyoutFlater=(LayoutFlater)
mContext.getSystemService(Context.LAYOUT\u充气机\u服务);
View itemView=MLAyoutFlater.充气(R.layout.pager_项目、容器、,
假);
BitmapFactory.Options=new-BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.ARGB_8888;
位图=BitmapFactory.decodeFile(imgUrl.get(位置),选项);
TouchImageView imgview=(TouchImageView)
itemView.findViewById(R.id.imgEvent);
imgview.setImageBitmap(位图);
container.addView(itemView);
返回项目视图;
}
@凌驾
公共项(视图组容器、int位置、对象){
container.removeView((RelativeLayout)对象);
}
EventType\u Images.java:我已经将字符串imgUrl转换为arraylist,对吗?如果不对,请告诉我正确的方法

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);
}
公共类事件类型\u图像扩展活动{
TextView-txtHeader;
字符串passedData,passedData 1;
公共静态字符串imgURL;
字符串图像;
查看页面查看页面;
ArrayList imgList;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.eventtype_图像);
passedData=getIntent().getStringExtra(“名称”);
txtHeader=(TextView)findViewById(R.id.txtHeader);
passedData1=getIntent().getStringExtra(“类型”);
txtHeader.setText(passedData1);
images=getIntent().getStringExtra(“imageName”);
viewPager=(viewPager)findViewById(R.id.pager);
imgURL=”http://197.188.1.50:8082/Server/Gallery/“+图像;
imgList=newarraylist(Arrays.asList(imgURL));
新建MyAsyncTask(此,
执行(“http://197.188.1.50:8082/Server/Gallery/“+图像);
}
}
类MyAsyncTask扩展了AsyncTask{
私人语境;
私人视页机视页机;
定制PageRadapter适配器;
公共MyAsyncTask(上下文,ArrayList imgList){
this.context=上下文;
this.viewPager=viewPager;
this.imgList=imgList;
}
受保护对象doInBackground(字符串…参数){
URL imgUrl=null;
InputStream=null;
HttpURLConnection conn=null;
试一试{
imgUrl=新URL(EventType_Images.imgUrl);
conn=(HttpURLConnection)imgUrl.openConnection();
conn.setDoInput(真);
连接();
is=conn.getInputStream();
bm=BitmapFactory.decodeStream(is);//转换为位图
}捕获(例外e){
e、 printStackTrace();
}
返回适配器;
}
@凌驾
受保护的void onPostExecute(对象结果){
dialog.dismise();
//imgview.setImageBitmap(bm);
适配器=新的CustomPagerAdapter(上下文,imgList);
viewPager.setAdapter(适配器);
super.onPostExecute(结果);
}
错误

02-24 17:00:57.492:E/BitmapFactory(12895):无法解码流:java.io.FileNotFoundException:/http:/197.188.1.50:8082/Server/Gallery/1.jpg2.jpg:打开失败:eNote(没有此类文件或目录) 图像名称也一个接一个地出现


感谢您

假设您的日志条目在此处准确列出,URL似乎是错误的:

java.io.FileNotFoundException: 
/http:/197.188.1.50:8082/Server/Gallery/[1.jpg2.jpg
这看起来不是一个正确的URL

此外,您还询问将字符串转换为
ArrayList
是否正确。此代码非常奇怪。您在活动中保留一个列表作为静态成员,并且它被
AsyncTask
引用,而不是仅仅将其传递给
AsyncTask
doInBackground
支持参数rs传递到任务中-使用它

您通常不应该将活动中的任何数据作为静态成员公开,因为它不尊重活动生命周期等


首先,我要验证您传递的URL是否正确。修复您的
AsyncTask
,以便传递所需的参数,删除静态公开的成员,并在
doInBackground
中添加一个日志项,以打印您尝试连接的URL。因为我们不知道您的“图像”是什么intent中的参数保持不变,很难说您真正传递到该列表中的是什么-但在任何情况下,它始终是一个包含一个元素的列表。

看起来您有一个
[
在图像名称前面。它看起来也有多个文件名,即
1.jpg2.jpg
所以请整理
图像的内容是的,先生,在我的数据库中,我有4到5个分支名称,如计算机、电子产品等,这就是为什么我需要一个viewpager,这样,如果我在同一分支中插入图像,我可以在这个库的ViewPagerry的帮助下查看它[是错误的,先生,我已经编辑了代码,但是我在viewpager.setadapter(adapter)中遇到了一个错误;这个URL现在也很奇怪,