android工程之WIFI环境下获取本地文件并压缩

1.介绍:
根据手机当前网络判断是否进入文件管理器界面,获取文件(视频)后如果需要上传,则需要先压缩并保证质量。
2.思路:

1.涉及到网络,则需要赋予网络权限:
<uses-permission android:name="android.permission.INTERNET" />
2.涉及到访问本地文件,则需要赋予访问权限,包括相机、文件管理器等:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
3.涉及文件的读写任务,则需要赋予读写权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
4.上传视频前的压缩准备,则需要赋予android自带的压缩工具使用权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

3.涉及到jar包工具(build gradle中直接导入):

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    // okhttp3的依赖
    implementation 'com.squareup.okhttp3:okhttp:3.8.1'
    implementation 'com.squareup.okio:okio:1.12.0'
    // gson解析的依赖
    implementation 'com.google.code.gson:gson:2.8.0'
    // Retrofit2的依赖
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    //视频压缩工具
    implementation'com.iceteck.silicompressorr:silicompressor:2.2.1'
}

4.首先画MainActivity的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开相机"
        android:textAllCaps="false"
        android:textSize="25sp"
        android:textColor="@color/colorAccent"/>

    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开相册"
        android:textAllCaps="false"
        android:textSize="25sp"
        android:textColor="@color/colorAccent"/>

    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取视频"
        android:textAllCaps="false"
        android:textSize="25sp"
        android:textColor="@color/colorAccent"/>



</LinearLayout>

5.其次写MainActivity功能逻辑代码(根据网络的类型做相应的处理–调用网络类型获取工具类)

package com.example.uploaddemo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import util.IfNetType;

public class MainActivity extends AppCompatActivity {
    //没有网络连接
    public static final int NETWORN_NONE = 0;
    //wifi连接
    public static final int NETWORN_WIFI = 1;
    //手机网络数据连接类型
    public static final int NETWORN_2G = 2;
    public static final int NETWORN_3G = 3;
    public static final int NETWORN_4G = 4;
    public static final int NETWORN_MOBILE = 5;

    //声明组件
    private Button btn1,btn2,btn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取组件
        btn1 = findViewById(R.id.btn_1);
        btn2 = findViewById(R.id.btn_2);
        btn3 = findViewById(R.id.btn_3);

        //获取点击事件对象
        onClickCase onClickCase = new onClickCase();
        //设置点击事件
        btn1.setOnClickListener(onClickCase);
        btn2.setOnClickListener(onClickCase);
        btn3.setOnClickListener(onClickCase);

    }

    //继承点击事件工具类
    class onClickCase implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_1:
                    Log.d("qwe","点击了");
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, 200);
                    break;
                case R.id.btn_2:
                    Intent intent1 = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent1, 200);
                    break;
                case R.id.btn_3:
                    int icont = IfNetType.getNetworkState(MainActivity.this);
                    switch (icont){
                        case NETWORN_NONE:
                            Toast.makeText(MainActivity.this,"当前没有网络!请确认网络!",Toast.LENGTH_LONG).show();
                            break;
                        case NETWORN_WIFI:
                            Toast.makeText(MainActivity.this,"当前连接的是WIFI网络",Toast.LENGTH_LONG).show();
                            break;
                        case NETWORN_2G:
                            Toast.makeText(MainActivity.this,"当前连接的是2G网络",Toast.LENGTH_LONG).show();
                            break;
                        case NETWORN_3G:
                            Toast.makeText(MainActivity.this,"当前连接的是3G网络",Toast.LENGTH_LONG).show();
                            break;
                        case NETWORN_4G:
                            Toast.makeText(MainActivity.this,"当前连接的是4G网络",Toast.LENGTH_LONG).show();
                            break;
                        case NETWORN_MOBILE:
                            Toast.makeText(MainActivity.this,"当前连接的是移动网络",Toast.LENGTH_LONG).show();
                            break;
                    }
                    if(icont ==NETWORN_NONE){//没有网络
                        Toast.makeText(MainActivity.this,"当前没有网络!请确认网络!",Toast.LENGTH_LONG).show();
                    }else{
                        if(icont != NETWORN_WIFI){//如果不是WIFI,需要提示
                            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                            builder.setTitle("警告!!!").setMessage("当前连接的不是WIFI网络!是否还要继续").setPositiveButton("继续",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Intent intent2 = new Intent(MainActivity.this,videoActivity.class);
                                            startActivity(intent2);
                                        }
                                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(MainActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
                                }
                            }).show();
                        }else{//如果是WIFI就直接跳
                            Intent intent2 = new Intent(MainActivity.this,videoActivity.class);
                            startActivity(intent2);
                        }
                    }
                    break;
            }
        }
    }



}

6.接着画获取文件和压缩文件相应布局:

<?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:id="@+id/ll_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/reduce_before_file_path"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="压缩前长度:"
                android:textSize="15sp"
                android:textColor="@color/colorPrimary"/>
            <EditText
                android:id="@+id/before_file_path_length"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FB4005"
                android:textSize="15dp"
                android:hint="压缩前长度"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="压缩后长度:"
                android:textSize="15sp"
                android:textColor="@color/colorPrimary"
                android:layout_marginLeft="10dp"/>
            <EditText
                android:id="@+id/after_file_path_length"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FB4005"
                android:textSize="15dp"
                android:hint="压缩后长度"/>
        </LinearLayout>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_before_file_path"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="视频压缩前的地址"
            android:gravity="center"
            android:textSize="20sp"
            android:textColor="@color/colorPrimary"/>
        <EditText
            android:id="@+id/reduce_before_file_path"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_below="@+id/tv_before_file_path"
            android:layout_gravity="center_vertical"
            android:textColor="#FB4005"
            android:textSize="18dp"
            android:hint="视频压缩前的地址"/>

        <TextView
            android:id="@+id/tv_after_file_path"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/reduce_before_file_path"
            android:text="视频压缩后的地址"
            android:gravity="center"
            android:textSize="20sp"
            android:textColor="@color/colorPrimary"/>
        <EditText
            android:id="@+id/reduce_after_file_path"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_below="@id/tv_after_file_path"
            android:layout_gravity="center_vertical"
            android:textColor="#FB4005"
            android:textSize="18dp"
            android:hint="视频压缩后的地址"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/video_select"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="选择视频"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:textSize="20sp"/>
        <Button
            android:id="@+id/video_upload"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:layout_below="@+id/video_select"
            android:layout_marginTop="15dp"
            android:text="点击压缩"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:textSize="20sp"/>
    </RelativeLayout>
</LinearLayout>

7.写videoActivity逻辑代码(调用本地文件管理器和调用压缩工具)

package com.example.uploaddemo;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.iceteck.silicompressorr.SiliCompressor;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import util.GetPathFromUri4kitkat;

public class videoActivity extends AppCompatActivity {
    public final static int VEDIO_KU = 101;
    private String file_path = "";//文件路径
    private String new_file_path = "";//新文件路径
    private long file_length = 0;//文件长度
    private long new_file_length = 0;//新文件长度
    private TextView before_file_path_length,after_file_path_length,reduce_before_file_path,reduce_after_file_path;
    private Button video_select,video_upload;

    /**
     * 子线程无法setText,所以使用Handler的Message.sendMessage方法传值
     */
    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            if(msg.what == 1){
                reduce_after_file_path.setText(new_file_path);
                after_file_path_length.setText(new_file_length+"");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        //设置标题
        getSupportActionBar().setTitle("视频上传");
        //true:activity左上角出现一个小图标,点击可返回上一级。false:没有
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        video_select = findViewById(R.id.video_select);//选择视频按钮
        video_upload = findViewById(R.id.video_upload);//选择上传按钮
        reduce_before_file_path = findViewById(R.id.reduce_before_file_path);
        before_file_path_length = findViewById(R.id.before_file_path_length);
        reduce_after_file_path = findViewById(R.id.reduce_after_file_path);
        after_file_path_length = findViewById(R.id.after_file_path_length);

        //打开文件夹点击事件
        video_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                before_file_path_length.setText("");
                after_file_path_length.setText("");
                reduce_before_file_path.setText("");
                reduce_after_file_path.setText("");
                seleteVedio();//进入相册
            }
        });

        //上传点击事件
        video_upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(file_path.equals(""))
                    Toast.makeText(videoActivity.this, "请选择视频后,再点击上传!", Toast.LENGTH_LONG).show();
                else {
                    Log.d("qwe","压缩前file长度:"+new File(file_path).length());
                    Log.d("qwe","压缩前file地址:"+file_path);

                    reduceFile();//压缩视频

//                    uploadFile(file);//上传视频
                }
            }
        });
    }

    /**
     * 打开系统的文件选择器
     */
    public void seleteVedio() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");//设置类型,我这里是任意类型,任意后缀的可以这样写。
        startActivityForResult(intent, VEDIO_KU);
    }
    /**
     * 选择回调
     * 获取文件的真实路径
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Log.d("qwe","------------------分割线------------------");
        if (requestCode == videoActivity.VEDIO_KU) {
            if (resultCode == RESULT_OK) {
                Uri uri = intent.getData();
                Log.d("qwe","获取uri:"+uri.toString());
                if (uri != null) {
                    String path = GetPathFromUri4kitkat.getPath(this,uri);
                    if (path != null) {
                        File file = new File(path);
                        if (file.exists()) {
                            String upLoadFilePath = file.toString();
                            String upLoadFileName = file.getName();

                            file_path = upLoadFilePath;
                            file_length = new File(file_path).length();

                            reduce_before_file_path.setText(file_path);
                            before_file_path_length.setText(file_length+"");

                            Log.d("qwe","转换后FilePath上传真实路径为:"+upLoadFilePath);
                            Log.d("qwe","转换后FileName上传真实名称为:"+upLoadFileName);
                        }
                    }
                }
            }
        }
    }

    /**
     * 视频压缩
     */
    public void reduceFile(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //第一个参数:视频源文件路径,第二个参数:压缩后视频保存的路径
                    String path = SiliCompressor.with(videoActivity.this).compressVideo(videoActivity.this.file_path,
                            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
                    long length = new File(path).length();

                    new_file_path = path;
                    new_file_length = length;

                    Log.d("qwe","压缩后file长度:"+new_file_length);
                    Log.d("qwe","压缩后file地址:"+new_file_path);

                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 使用OkHttp上传文件
     * @param file
     */
    public void uploadFile(File file) {
        OkHttpClient client = new OkHttpClient();
        MediaType contentType = MediaType.parse("text/csv"); // 上传文件的Content-Type
        RequestBody body = RequestBody.create(contentType, file); // 上传文件的请求体
        Request request = new Request.Builder().addHeader("Connection","close")
                .url("http://182.254.222.200:8080/audio") // 上传地址
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // 文件上传成功
                if (response.isSuccessful()) {
                    Log.d("qwe", "onResponse成功: " + response.body().string());
                } else {
                    Log.d("qwe", "onResponse失败: " + response.message());
                }
            }

            @Override
            public void onFailure(Call call, IOException e) {
                // 文件上传失败
                Log.d("qwe", "onFailure: " + e.getMessage());
            }
        });
    }
}

8.通过本地文件管理器获取文件真实地址工具类

package util;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

/**
 * 通过资源管理器获取文件真实路径
 */
public class GetPathFromUri4kitkat {
    /**
     * android4.4版本之前和之后的文件管理器的路径不一样
     * 专为Android4.4设计的从Uri获取文件绝对路径,以前的方法已不好使
     */
    @SuppressLint("NewApi")
    public static String getPath(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }

                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[] { split[1] };

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context
     *            The context.
     * @param uri
     *            The Uri to query.
     * @param selection
     *            (Optional) Filter used in the query.
     * @param selectionArgs
     *            (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    public static String getDataColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = { column };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }

    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }
}

9.获取手机当前网络状态:

package util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * 获取网络连接的工具类
 * Created by chengguo on 2016/3/17.
 */
public class IfNetType {
    //没有网络连接
    public static final int NETWORN_NONE = 0;
    //wifi连接
    public static final int NETWORN_WIFI = 1;
    //手机网络数据连接类型
    public static final int NETWORN_2G = 2;
    public static final int NETWORN_3G = 3;
    public static final int NETWORN_4G = 4;
    public static final int NETWORN_MOBILE = 5;
    /**
     * 获取当前网络连接类型
     * @param context
     * @return
     */
    public static int getNetworkState(Context context) {
        //获取系统的网络服务
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //如果当前没有网络
        if (null == connManager)
            return NETWORN_NONE;
        //获取当前网络类型,如果为空,返回无网络
        NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
        if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
            return NETWORN_NONE;
        }
        // 判断是不是连接的是不是wifi
        NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (null != wifiInfo) {
            NetworkInfo.State state = wifiInfo.getState();
            if (null != state)
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    return NETWORN_WIFI;
                }
        }
        // 如果不是wifi,则判断当前连接的是运营商的哪种网络2g、3g、4g等
        NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (null != networkInfo) {
            NetworkInfo.State state = networkInfo.getState();
            String strSubTypeName = networkInfo.getSubtypeName();
            if (null != state)
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    switch (activeNetInfo.getSubtype()) {
                        //如果是2g类型
                        case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g
                        case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g
                        case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g
                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                        case TelephonyManager.NETWORK_TYPE_IDEN:
                            return NETWORN_2G;
                            //如果是3g类型
                        case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g
                        case TelephonyManager.NETWORK_TYPE_UMTS:
                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                        case TelephonyManager.NETWORK_TYPE_HSPA:
                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                            return NETWORN_3G;
                            //如果是4g类型
                        case TelephonyManager.NETWORK_TYPE_LTE:
                            return NETWORN_4G;
                        default:
                            //中国移动 联通 电信 三种3G制式
                            if (strSubTypeName.equalsIgnoreCase("TD-SCDMA") || strSubTypeName.equalsIgnoreCase("WCDMA") || strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                                return NETWORN_3G;
                            } else {
                                return NETWORN_MOBILE;
                            }
                    }
                }
        }
        return NETWORN_NONE;
    }
}

10.以上就是根据网络状态判断是否进入文件管理器页面的代码,需要注意的是—视频压缩的线程是子线程,然而子线程并不具备改变控件内容的效果(.setText(xxx)。。。报错),所以需要使用Handler的Message.sendMessage方法传值:

private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            if(msg.what == 1){
                reduce_after_file_path.setText(new_file_path);
                after_file_path_length.setText(new_file_length+"");
            }
        }
    };

接着在子线程-视频压缩的方法中:

Message message = new Message();
message.what = 1;
handler.sendMessage(message);

这样,就相当于直接在线程的run里面调用 .setText(xxx)了。

11.以下是demo结构图:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

//上传的功能需要结合HTTP协议去做,暂时还没写

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android 1.5.1 beta 大小:6.3MB 更新:2014-12-24 Android 4.0.4 自同步是一款由国内创业团队推出的局域网P2P免费文件同步工具。处在同一个局域网下的多台电脑,可通过自同步来建立电脑里文件夹之间的同步关系,做到文件夹中文件的数量、内容相一致,并且不需要云和外网。此外,自同步特有的密钥加密更保证了数据的安全性。并且,其特有的分享功能又可以做到轻轻松松将同步目录分享给周围的朋友们。 软件特点 1. 无需网盘,局域同步 与市面上流行的网盘不同,自同步不需要您的设备时时刻刻连接互联网完成文件同步工作,而只需要在同一个局域网内就能完成文件同步工作。 2. 实时同步,多台互联 处于同步中的目录,只要其中一个目录发生变化,如文件/目录添加、修改、删除等操作,那么与该目录建立同步关系的其它目录也会迅速反应,进行相应的修改,保持与原目录的文件一致;此外,只需将文件放到同步目录,程序将自动上传这些文件,同时其它电脑登陆自同步时自动下载到新电脑,实现多台电脑的文件同步。 3. 目录分享,轻松同步 同步目录分享功能,可以轻松将同步目录分享给周围的朋友(无需同步口令相同)。 4. 密钥加密,保护数据 在局域网内采用AES加密方式传输数据并建立密钥,防止处于同一局域网内的非法设备窃取数据。 5. 优化传输,极速速度 千兆路由的局域网环境下传输速度最高可达70MB/s,同步过程犹如硬盘间的复制粘贴一样方便快捷。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哎呦喂O_o嗨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值