Android端使用okhttp網絡框架下載服務器端的視頻

畢設項目中做了視頻下載功能,只需要在Android端能拿到視頻文件的路徑就行。

首先在video_details.xml佈局文件添加一個按鈕,略

對應的VideoDetails.java文件

點擊按鈕後,獲取視頻路徑,調用get_file(String video_path)方法下載視頻,同時彈出AlertDialog彈框,提醒用戶正在下載中。

    AlertDialog mydlg2;
    View view2;
 String video_path = NetConfig.VIDEOIP+list.get(0).getVideo_filename();
                get_file(video_path);
                view2 = LayoutInflater.from(VideoDetails.this).inflate(R.layout.video_details_dialog2,null);
                System.out.println("view2-------------"+view2);
                mydlg2 = new AlertDialog.Builder(VideoDetails.this)
                        .setView(view2)
                        .create();
                mydlg2.show();

get_file(String video_path)方法,使用okhttp網絡框架,下載文件到根目錄下。下載成功,則彈框隱藏。

 //下載文件
    public void get_file(String URL){
        OkHttpUtils
                .get()
                .url(URL)
                .build()
                .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(),list.get(0).getVideo_filename()) {
                    @Override
                    public void inProgress(float v, long l) {
                        Log.e("","inProgress : "+ (int) (100 * v ));
                        if((int) 100* v ==100){
                            mydlg2.hide();
                            Toast.makeText(VideoDetails.this,"下載完成!",Toast.LENGTH_SHORT).show();
                            Toast.makeText(VideoDetails.this,"下載目錄爲根目錄!",Toast.LENGTH_LONG).show();
                        }
                    }
                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("","onError : "+ e.getMessage());
                    }
                    @Override
                    public void onResponse(File file) {
                        Log.e("","onResponse : "+ file.getAbsolutePath());
                    }
                });
    }

彈框佈局video_details_dialog2.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_margin="20dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/tv_1_upload_video_dialog"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="請稍等"
                android:gravity="center"
                android:textSize="20sp"
                android:textColor="@color/bule"/>
            <TextView
                android:id="@+id/tv_2_upload_video_dialog"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="視頻正在下載中。。。。"
                android:gravity="center"
                android:textSize="20sp"
                android:textColor="@color/bule"/>
        </LinearLayout>
        <pl.droidsonroids.gif.GifImageView
            android:id="@+id/gv_video_details_dialog2"
            android:layout_width="280dp"
            android:layout_height="200dp"
            android:layout_marginTop="10dp"
            android:layout_gravity="center"
            android:scaleType="fitXY"
            android:src="@drawable/upload_file_gif1"/>
    </LinearLayout>

</LinearLayout>

樣式:

因爲使用了.gif格式的圖片,所以需要在build.gradle中添加

implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.15'

OK!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章