第一步MainActivity.java
package com.example.myoo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
//android:usesCleartextTraffic="true" 这句话非常重要
private Button btnsave;
private EditText editurl;
Handler handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
if(msg.what == 0x123)
{
Toast.makeText(getApplicationContext(), "开始下载", Toast.LENGTH_SHORT).show();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsave = (Button) findViewById(R.id.btnsave);
editurl = (EditText) findViewById(R.id.editurl);
btnsave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread()
{
@Override
public void run() {
String path = editurl.getText().toString();
try {
DownLoadService.downLoad(path, MainActivity.this);
} catch (Exception e) {e.printStackTrace();}
handler.sendEmptyMessage(0x123);
}
}.start();
}
});
}
}
第二步activity-main.xml
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入图片的网络地址" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editurl"
android:text="http://10.13.20.32:8080/Test/db.zip"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnsave"
android:text="缓存文件"
/>
</LinearLayout>
第三步 androidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:usesCleartextTraffic="true"
第四步:
package com.example.myoo;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import android.content.Context;
import android.widget.Toast;
public class DownLoadService {
public static void downLoad(String path,Context context)throws Exception
{
URL url = new URL(path);
InputStream is = url.openStream();
//截取最后的文件名
String end = path.substring(path.lastIndexOf("."));
//打开手机对应的输出流,输出到文件中
OutputStream os = context.openFileOutput("Cache_"+System.currentTimeMillis()+end, Context.MODE_PRIVATE);
byte[] buffer = new byte[1024];
int len = 0;
//从输入六中读取数据,读到缓冲区中
while((len = is.read(buffer)) > 0)
{
os.write(buffer,0,len);
}
//关闭输入输出流
is.close();
os.close();
}
}
Android studio 53 文件下载
最新推荐文章于 2024-05-27 23:34:26 发布