在Android

时间:2016-03-24 09:38:06

标签: android imageview

我正在尝试在回收站视图中显示用户详细信息。

当我尝试显示我的图像时,它无法正常工作。我已经尝试了所有可能性。以下是我的适配器类。

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
   private LinkedList<HashMap<String, Object>> myDataSet;
   Bitmap bitmap;
   MyAdapter.ViewHolder viewHolder;
   String image;

public MyAdapter(LinkedList<HashMap<String, Object>> values) {
    myDataSet = values;
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView userIdView;
    public TextView jobTitleNameView;
    public TextView employeeCodeView;
    public ImageView imageView;

    public ViewHolder(View itemView) {
        super(itemView);
        userIdView = (TextView)itemView.findViewById(R.id.userId);
        jobTitleNameView = (TextView)itemView.findViewById(R.id.jobTitleName);
        employeeCodeView = (TextView)itemView.findViewById(R.id.employeeCode);
    }
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(
            parent.getContext());
    View v = inflater.inflate(R.layout.activity_listview, parent, false);
    viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    final HashMap<String, Object> userDetails = myDataSet.get(position);
    String userId = (String) userDetails.get("userId");
    String jobTitleName = (String) userDetails.get("jobTitleName");
    String employeeCode = String.valueOf(userDetails.get("employeeCode"));
    image = (String) userDetails.get("image");

    ImageDownloader imageDownloader = new ImageDownloader();
    imageDownloader.execute(viewHolder);


    holder.userIdView.setText(userId);
    holder.jobTitleNameView.setText(jobTitleName);
    holder.employeeCodeView.setText(employeeCode);
}

@Override
public int getItemCount() {
    return myDataSet.size();
}

这是在本课程中编写的用于下载图像的类。

private class ImageDownloader extends AsyncTask<ViewHolder,ViewHolder,ViewHolder>{

    @Override
    protected ViewHolder doInBackground(ViewHolder... params) {
        viewHolder = params[0];

        URL url = null;
        try {
            url = new URL(image);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return viewHolder;
    }

    @Override
    protected void onPostExecute(ViewHolder result){
        viewHolder.imageView.setImageBitmap(bitmap);
    }
   }
 }

有人可以帮我这个....

请查看下面的日志

    03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err: java.net.ConnectException: failed to connect to www.scri8e.com/208.87.227.250 (port 80): connect failed: ETIMEDOUT (Connection timed out)
    03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
    03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
    03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
    03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err:     at java.net.Socket.connect(Socket.java:882)
    03-24 16:27:48.421 19806-20764/com.example.rini.listviewandroid W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:152)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at com.example.rini.listviewandroid.MyAdapter$ImageDownloader.doInBackground(MyAdapter.java:94)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at com.example.rini.listviewandroid.MyAdapter$ImageDownloader.doInBackground(MyAdapter.java:77)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at java.lang.Thread.run(Thread.java:818)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at libcore.io.Posix.connect(Native Method)
    03-24 16:27:48.422 19806-20764/com.example.rini.listviewandroid W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
    03-24 16:27:48.423 19806-20764/com.example.rini.listviewandroid W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
    03-24 16:27:48.423 19806-20764/com.example.rini.listviewandroid W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
    03-24 16:27:48.423 19806-20764/com.example.rini.listviewandroid W/System.err:   ... 17 more

4 个答案:

答案 0 :(得分:2)

虽然它不是一个直接的解决方案,但我建议使用像毕加索和格莱德这样的图书馆吗?它将显着减少代码行数。以下是图书馆的一些链接

毕加索:http://square.github.io/picasso/

Glide:https://futurestud.io/blog/glide-getting-started

你的onBindViewHolder会看起来像这样

@Override 
public void onBindViewHolder(ViewHolder holder, final int position) {
    final HashMap<String, Object> userDetails = myDataSet.get(position);
    String userId = (String) userDetails.get("userId");
    String jobTitleName = (String) userDetails.get("jobTitleName");
    String employeeCode =     String.valueOf(userDetails.get("employeeCode"));
    image = (String) userDetails.get("image");

    //With Picasso 
    Picasso.with(context).load(image).into(viewHolder.imageView);

    //With Glide 
    Glide.with(context).load(image).into(viewHolder.imageView);

    holder.userIdView.setText(userId);
    holder.jobTitleNameView.setText(jobTitleName);
    holder.employeeCodeView.setText(employeeCode);
} 

答案 1 :(得分:0)

尝试像这样修改ImageDownloader。我不确定,但它应该有用;

private class ImageDownloader extends AsyncTask<Void,Void,Bitmap> {
    private ViewHolder viewHolder;
    private String image;

    public ImageDownloader(ViewHolder viewHolder, String image) {
        this.viewHolder = viewHolder;
        this.image = image;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {

        URL url = null;
        try {
            url = new URL(image);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            return BitmapFactory.decodeStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap){
        if(bitmap != null) {
            viewHolder.imageView.setImageBitmap(bitmap);
        }
    }
}

比适配器:

ImageDownloader imageDownloader = new ImageDownloader(viewHolder, image);
imageDownloader.execute();

答案 2 :(得分:0)

像这样解码位图可能会有问题:

 bitmap = BitmapFactory.decodeStream(inputStream);

如果某些字节错误,结果将返回null。

您可以尝试如下:

private static Bitmap urlFetch(Context context, String url) {
    try {
        URL u = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) u.openConnection();
        int rspCode = conn.getResponseCode();
        if (rspCode != 200) {
            return null;
        }
        InputStream inputStream = null;
        inputStream = conn.getInputStream();
        long total = conn.getContentLength();
        try {
            return BitmapFactory.decodeStream(new FlushedInputStream(
                        inputStream));
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return null;
}


static class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                int b = read();
                if (b < 0) {
                    break; // we reached EOF
                } else {
                    bytesSkipped = 1; // we read one byte
                }
            }
            totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
}

基本上它会跳过错误的字节以防止所有位图出错。

顺便说一句,Imageloader是一个复杂的方法,你需要考虑内存消耗(处理位图很容易导致OOM),缓存并重用位图为file或{{1} ,同时下载多张图片而不降低UX(线程池),所以我建议你使用第三方库来处理这个工作。 Universe-image-loader是个不错的选择。

答案 3 :(得分:0)

这是一个Picasso library .xactly意味着你正在寻找什么。我一直在使用它,因为很长,高度可定制。你甚至可以选择缓存图像,以便更快地从网络加载或从存储。就像那样简单

  

Picasso.with(上下文).load(&#34; http://i.imgur.com/DvpvklR.png&#34)。到(ImageView的);