如何将URL图像下载到现有列表视图

时间:2017-01-22 18:50:55

标签: java android url imageview

我有自己的基本适配器的自定义列表视图。它目前有一个标题和描述。我喜欢从网址添加图片。但我不知道如何调整代码来添加添加图像。

我已按下列表视图中的项目时使用异步任务下载文件了我可以重复使用此方法添加图像吗?

我的异步任务仅在文件下载

class DownloadFileFromURL extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread
     * Show Progress Bar Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(progress_bar_type);
    }

    /**
     * Downloading file in background thread
     * */
    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length
            int lenghtOfFile = conection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file
            OutputStream output = new FileOutputStream("/sdcard/Download/"+newnamestring);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress(""+(int)((total*100)/lenghtOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }

    /**
     * Updating progress bar
     * */
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        pDialog.setProgress(Integer.parseInt(progress[0]));
    }

    /**
     * After completing background task
     * Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after the file was downloaded

        dismissDialog(progress_bar_type);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + newnamestring)), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);




    }

}

我的自定义基础适配器

class dataListAdapter extends BaseAdapter {
    String[] Title, Detail;


    dataListAdapter() {
        Title = null;
        Detail = null;

        ;

    }

    public dataListAdapter(String[] text, String[] text1 ) {
        Title = text;
        Detail = text1;





    }

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

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View row;

        row = inflater.inflate(R.layout.custom, parent, false);
        TextView title, detail;
        title = (TextView) row.findViewById(R.id.titleapp);
        detail = (TextView) row.findViewById(R.id.detail);
        title.setText(Title[position]);
        detail.setText(Detail[position]);
        return (row);
    }


}

1 个答案:

答案 0 :(得分:1)

如果你想从URL下载图像并将其显示到你的ImageView中,你可以做的最好的事情是使用Glide库,这个库可以下载Image并显示到你的imageView并缓存它以便再使用一次, 以及在添加到项目后如何使用它; Glide.with(mContext).load(imageUrl).into(yourImageView);