Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从服务器下载视频文件并保存Sd卡?_Android_Android Listview_Download_Progress Bar - Fatal编程技术网

Android 如何从服务器下载视频文件并保存Sd卡?

Android 如何从服务器下载视频文件并保存Sd卡?,android,android-listview,download,progress-bar,Android,Android Listview,Download,Progress Bar,我开发下载应用程序,当我点击保存按钮,然后在后台下载过程,但按钮点击不显示正确的位置。我有一个列表视图,其中我膨胀了一行包含Imageview和按钮和进度条现在我想处理按钮的点击事件,它编码在适配器中,我也能够得到按钮的位置。但在这里,当我点击按钮时,我正在更改按钮的图像,它工作正常。问题是当我滚动视图时,它再次更改按钮的图像,因为getview()方法每次都会更改其循环视图。以及如何下载视频并保存sd卡。我的代码如下: public class TestHopeListNew extends

我开发下载应用程序,当我点击保存按钮,然后在后台下载过程,但按钮点击不显示正确的位置。我有一个列表视图,其中我膨胀了一行包含Imageview和按钮和进度条现在我想处理按钮的点击事件,它编码在适配器中,我也能够得到按钮的位置。但在这里,当我点击按钮时,我正在更改按钮的图像,它工作正常。问题是当我滚动视图时,它再次更改按钮的图像,因为getview()方法每次都会更改其循环视图。以及如何下载视频并保存sd卡。我的代码如下:

public class TestHopeListNew extends Activity {
    ListView lv;
    ArrayList<Url_Dto> list = new ArrayList<Url_Dto>();
    MyListAdapter adtf;
    public static String prf_date_view = "";
    String str_start;
    Button mainDownloadBtn;
    public static SQLiteDatabase db;
    ProgressBar freePr;
    String name;
    File download;
    int i = 0;
    private ArrayList<ProgressBarSeek> progreeSeekList;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_hope_list_new);
        progreeSeekList = new ArrayList<ProgressBarSeek>();
        list = DBAdapter.getUrl_Detail();
        Log.v("log_tag", "list  :: " + list.size());
        lv = (ListView) findViewById(R.id.main_list_meet);
        mainDownloadBtn = (Button) findViewById(R.id.not_shown);
        freePr = (ProgressBar) findViewById(R.id.progressBar1);
        adtf = new MyListAdapter(this);
        lv.setAdapter(adtf);
        //adtf.notifyDataSetChanged();
        SqliteAdpter dba = SqliteAdpter
                .getAdapterInstance(getApplicationContext());
        dba.createdatabase();
        db = dba.openDataBase();
        BusyExtMemory();
        mainDownloadBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                adtf.setAllDownload();
            }
        });

    }

    public class MyListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        ProgressBar pr;
        ProgressBar[] prArray = new ProgressBar[list.size()];
        Button cl, dl;
        ImageView im;
        DownloadFileFromURL downloadFileFromURL;


        public MyListAdapter(Context context) {
            mInflater = LayoutInflater.from(context);

        }

        public int getCount() {
            return list.size();
        }

        public Object getItem(int position) {
            return list.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public void setAllDownload() {
            if (prArray.length > 0) {
                for (int i = 0; i < prArray.length; i++) {
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    Log.v("log_tag", "list.get(i).url_video   "
                            + list.get(i).url_video);

                    downloadFileFromURL.execute(pr, list.get(i).url_video, i);
                }
            }
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            convertView = mInflater.inflate(R.layout.custome_list_view, null);

            cl = (Button) convertView.findViewById(R.id.cancle_sedual);
            dl = (Button) convertView.findViewById(R.id.download_sedual);
            pr = (ProgressBar) convertView.findViewById(R.id.listprogressbar);
            prArray[position] = pr;
            im = (ImageView) convertView.findViewById(R.id.list_image);
            im.setImageResource(list.get(position).images[position]);


            downloadFileFromURL = new DownloadFileFromURL(dl, cl);
            cl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Log.v("log_tag", "Cancle Button Click");

                    dl.setVisibility(View.VISIBLE);
                    cl.setVisibility(View.GONE);
                    downloadFileFromURL.cancel(true);
                     downloadFileFromURL.downloadFile();
                    pr.setProgress(0);
                }
            });

            dl.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    str_start = list.get(position).url_video;
                    dl.setVisibility(View.GONE);
                    cl.setVisibility(View.VISIBLE);
                    Log.v("log_tag", "Start Button Click ");
                    //downloadFileFromURL = new DownloadFileFromURL(dl, cl);
                    downloadFileFromURL.execute(pr, str_start, position);
                }
            });
            getProgress(pr, position, dl, cl);
            return convertView;
        }

    }

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

        int count = 0;
        ProgressDialog dialog;
        ProgressBar progressBar;
        int myProgress;
        int position;
        Button start, cancel;
        boolean download1 = false;

        public DownloadFileFromURL(Button start, Button cancel) {
            this.start = start;
            this.cancel = cancel;
        }

        /**
         * Before starting background thread Show Progress Bar Dialog
         * */

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ProgressBar progressBar;
            download1 = true;

        }

        public void downloadFile() {
            this.download1 = false;

        }

        @Override
        protected void onCancelled() {
            super.onCancelled();

        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected Integer doInBackground(Object... params) {
            int count;
            progressBar = (ProgressBar) params[0];
            position = (Integer) params[2];
            /*
             * try { Thread.sleep(500); } catch (InterruptedException e) {
             * e.printStackTrace(); }
             */
            try {

                URL url = new URL((String) params[1]);

                name = ((String) params[1]).substring(((String) params[1])
                        .lastIndexOf("/") + 1);
                // Log.v("log_tag", "name Substring ::: " + name);
                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);
                download = new File(Environment.getExternalStorageDirectory()
                        + "/download/");
                if (!download.exists()) {
                    download.mkdir();
                }
                String strDownloaDuRL = download + "/" + name;
                Log.v("log_tag", " down url   " + strDownloaDuRL);
                FileOutputStream output = new FileOutputStream(strDownloaDuRL);

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    if (this.download1) {
                        if (isCancelled()) {
                            break;
                        }

                        total += count;

                        // writing data to file
                        progressBar
                                .setProgress((int) ((total * 100) / lenghtOfFile));
                        output.write(data, 0, count);
                        setProgress(progressBar, position, start, cancel, this);
                    } else {
                        File delete = new File(strDownloaDuRL);
                        delete.delete();
                    }
                }
                // flushing output
                output.flush();

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

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

        }

        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... values) {

            super.onProgressUpdate(values);
            BusyExtMemory();

            // pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {

            Log.v("log", "login  ::: 4::: " + download);
            String videoPath = download + "/" + name;
            String chpName = name;
            Log.v("log_tag", "chpName  ::::" + chpName + "  videoPath "
                    + videoPath);
            db.execSQL("insert into videoStatus (chapterNo,videoPath) values(\""
                    + chpName + "\",\"" + videoPath + "\" )");

        }

    }

    private void setProgress(final ProgressBar pr, final int position,
            final Button Start, final Button cancel,
            final DownloadFileFromURL downloadFileFromURL) {
        ProgressBarSeek pbarSeek = new ProgressBarSeek();
        pbarSeek.setPosition(position);
        pbarSeek.setProgressValue(pr.getProgress());
        // Log.v("log_tag", position + "  progress  " + pr.getProgress());
        progreeSeekList.add(pbarSeek);
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.v("log_tag", "Cancle Button Click Set progress");
                Start.setVisibility(View.VISIBLE);
                cancel.setVisibility(View.GONE);
                downloadFileFromURL.cancel(true);
                // downloadFileFromURL.downloadFile();
                downloadFileFromURL.cancel(true);
                pr.setProgress(0);

            }
        });
        Start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.v("log_tag", "Start Button Click set Progress");
                str_start = list.get(position).url_video;
                Start.setVisibility(View.GONE);
                cancel.setVisibility(View.VISIBLE);
                Log.v("log_tag", "str_start  " + str_start);
                //
                // new DownloadFileFromURL().execute(str_start);
                DownloadFileFromURL downloadFileFromU = new DownloadFileFromURL(
                        Start, cancel);
                downloadFileFromU.execute(pr, str_start, position);
            }
        });
    }

    private void getProgress(ProgressBar pr, int position, Button dl, Button cl) {
        if (progreeSeekList.size() > 0) {
            for (int j = 0; j < progreeSeekList.size(); j++) {
                if (position == progreeSeekList.get(j).getPosition()) {
                    pr.setProgress(progreeSeekList.get(j).getProgressValue());
                    dl.setVisibility(View.GONE);
                    cl.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    public String TotalExtMemory() {
        StatFs statFs = new StatFs(Environment.getExternalStorageDirectory()
                .getAbsolutePath());
        int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        Log.v("log_tag", "TotalExtMemory    " + Total);
        String strI = Integer.toString(Total);
        return strI;
    }

    public String FreeExtMemory() {
        StatFs statFs = new StatFs(Environment.getExternalStorageDirectory()
                .getAbsolutePath());
        int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        String strI = Integer.toString(Free);
        Log.v("log_tag", "FreeExtMemory    " + strI);

        return strI;
    }

    public String BusyExtMemory() {
        StatFs statFs = new StatFs(Environment.getExternalStorageDirectory()
                .getAbsolutePath());
        int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        int Busy = Total - Free;
        String strI = Integer.toString(Busy);
        freePr.setProgress(Integer.parseInt(strI));
        Log.v("log_tag", "BusyExtMemory    " + strI);
        return strI;
    }
}
公共类TestHopeListNew扩展活动{
ListView lv;
ArrayList=新建ArrayList();
MyListAdapter-adtf;
公共静态字符串prf_date_view=“”;
字符串stru_开始;
按钮主下载;
公共静态数据库sqlitedb;
ProgressBar freePr;
字符串名;
文件下载;
int i=0;
私有ArrayList Progreeseklist;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u test\u hope\u list\u new);
progreeSeekList=新的ArrayList();
list=DBAdapter.getUrl_Detail();
Log.v(“Log_标记”,“list::”+list.size());
lv=(ListView)findViewById(R.id.main\u list\u meet);
mainDownloadBtn=(按钮)findViewById(未显示R.id);
freePr=(ProgressBar)findviewbyd(R.id.progressBar1);
adtf=新的MyListAdapter(本);
低压设置适配器(adtf);
//adtf.notifyDataSetChanged();
SqliteAdpter dba=SqliteAdpter
.getAdapterInstance(getApplicationContext());
创建数据库();
db=dba.openDataBase();
BusyExtMemory();
mainDownloadBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
adtf.setAllDownload();
}
});
}
公共类MyListAdapter扩展了BaseAdapter{
私人停车场;
ProgressBar公共关系;
ProgressBar[]prArray=新的ProgressBar[list.size()];
按钮cl,dl;
ImageView im;
DownloadFileFromURL DownloadFileFromURL;
公共MyListAdapter(上下文){
mInflater=LayoutInflater.from(上下文);
}
public int getCount(){
返回list.size();
}
公共对象getItem(int位置){
返回列表。获取(位置);
}
公共长getItemId(int位置){
返回位置;
}
public void setAllDownload(){
如果(prArray.length>0){
for(int i=0;icl.setVisibility(View.GONE);
v.setVisibility(View.GONE);