接口:
http://cdn.banmi.com/banmiapp/apk/banmi_330.apk
简单布局:
<LinearLayout
android:orientation="vertical"
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">
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/pb"
style="?android:progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
这里使用OkHttp进行下载
代码:
第一步权限:
private void initData() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)==
PackageManager.PERMISSION_GRANTED){
initData1();
}else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
100);
}
}
private void initData1() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
file = Environment.getExternalStorageDirectory();
}
}
第二布判断:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==100&&grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
initData1();
}
}
第三步解析:
private void initXia() {
OkHttpClient build = new OkHttpClient.Builder().build();
Request build1 = new Request.Builder()
.url("http://cdn.banmi.com/banmiapp/apk/banmi_330.apk")
.get()
.build();
Call call = build.newCall(build1);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
InputStream inputStream = body.byteStream();
long l = body.contentLength();
initStr(inputStream,file+"/b.apk",l);
}
});
}
private void initStr(InputStream inputStream, String s, long l) {
try {
FileOutputStream outputStream = new FileOutputStream(s);
byte[] arr=new byte[1024*10];
int a=0;
while ((a=inputStream.read(arr))!=-1){
outputStream.write(arr,0,a);
}
inputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
完整代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt;
private TextView tv;
private ProgressBar pb;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)==
PackageManager.PERMISSION_GRANTED){
initData1();
}else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
100);
}
}
private void initData1() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
file = Environment.getExternalStorageDirectory();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==100&&grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
initData1();
}
}
private void initView() {
bt = (Button) findViewById(R.id.bt);
tv = (TextView) findViewById(R.id.tv);
pb = (ProgressBar) findViewById(R.id.pb);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt:
initXia();
break;
}
}
private void initXia() {
OkHttpClient build = new OkHttpClient.Builder().build();
Request build1 = new Request.Builder()
.url("http://cdn.banmi.com/banmiapp/apk/banmi_330.apk")
.get()
.build();
Call call = build.newCall(build1);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
InputStream inputStream = body.byteStream();
long l = body.contentLength();
initStr(inputStream,file+"/b.apk",l);
}
});
}
private void initStr(InputStream inputStream, String s, long l) {
try {
FileOutputStream outputStream = new FileOutputStream(s);
byte[] arr=new byte[1024*10];
int a=0;
while ((a=inputStream.read(arr))!=-1){
outputStream.write(arr,0,a);
}
inputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
}