2015-02-24 70 views
2

我已经写了一个服务器端程序在JSP中插入图像到我的数据库和它的工作正常。 现在我想要加载从ViewPager(客户端)的URL中的图像 我写了一个CustomPagerAdapter类,并有一个活动,我想调用寻呼机适配器类,我得到一个错误无法解码流FileNotFoundException 请帮我弄清楚如何解决问题以及如何实施ViewPager如何实现一个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:在这里,我已转换的字符串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); 
} 

错误

02-24 17:00:57.492:E/BitmapFactory(12895):无法解码码流:java.io.FileNotFoundException:/ HTTP: /197.188.1.50:8082/Server/Gallery/1.jpg2.jpg:打开失败:ENOENT(没有这样的文件或目录) 图像名称也来一个其他

后感谢你

+0

它看起来像在图像名称前有一个'[''。它也看起来像你有不止一个文件名,即'1.jpg2.jpg'这样整理'图像' – RiggsFolly 2015-02-24 11:47:57

+0

的内容[错误,是我的数据库中的先生我有4到5个分支名称,如电脑,电子等,这就是为什么我想viewpager,如果我插入图像在同一个分支,我可以查看它与viewpager的帮助 – 2015-02-24 11:53:04

+0

试试这个库。 http://android-arsenal.com/details/1/3680 – AmniX 2016-06-13 05:30:38

回答

0

假设您的日志条目在这里准确地列出,该URL似乎是错误的:

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

这看起来不像一个合适的URL。

另外,您询问将字符串转换为ArrayList是否正确。这段代码很奇怪。您在列表中保留一个静态成员列表,并将其列入您的AsyncTask,而不是将它传递给您的AsyncTaskdoInBackground支持传递给任务的参数 - 使用它!

你通常应该永远不会从活动作为静态成员公开任何数据,因为它不尊重活性生命周期等

我会通过验证要传递正确的URL开始。修复您的AsyncTask,以便您传递所需的参数,删除静态显示的成员,并在doInBackground中添加一条日志条目,打印您尝试连接的URL。由于我们不知道您的“图片”参数在意图中所占的位置,因此很难说出您真正进入该列表的内容 - 但无论如何它始终是一个元素列表。

+0

先生的[是错误的,先生我编辑了代码,但我在viewpager中得到一个错误。setadapter(适配器);/xxx'的 代替 'HTTP: – 2015-02-24 12:25:57

+0

嘛URL现在是一样陌生,它以斜线'/ HTTP 开始// xxx' 等 请张贴更多的细节,回顾一下你正在尝试完成与该列表等 – JHH 2015-02-24 12:36:47

+0

该网址工作正常先生,但我得到onpostexecute错误,如果网址错了我wouldnt有我的doinbackground方法内连接 – 2015-02-24 12:39:09

相关问题