源码地址:github
[Android6.0之前]
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
private void copyFile() {
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = context.getAssets().open("app-debug.apk");
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {// 检查是否有存储卡
dir = Environment.getExternalStorageDirectory() + "/ceshi/";
File dirFile = new File(dir);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
File apk = new File(dir + "app-debug.apk");
if (!apk.exists()) {
apk.createNewFile();
}
outputStream = new FileOutputStream(apk);
byte[] buffer = new byte[1024];
int byteCount = 0;
while ((byteCount = inputStream.read(buffer)) != -1) {// 循环从输入流读取
// buffer字节
outputStream.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
}
outputStream.flush();// 刷新缓冲区
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 打开apk
*
* @param context
* @param pathName 文件路径
*/
public static void openFile(Context context, String pathName) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
}
[Android6.0]
compile 'com.yanzhenjie:permission:1.0.5'
TIP:目前开源的请求权限库比较多,选择其一就好。
if (AndPermission.hasPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
)) {
// 有权限,直接do anything.
copyFile();
} else {
// 申请权限。
AndPermission.with(MainActivity.this)
.requestCode(101)
.permission(Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
.send();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
// 只需要调用这一句,其它的交给AndPermission吧,最后一个参数是PermissionListener。
AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, listener);
}
private PermissionListener listener = new PermissionListener() {
@Override
public void onSucceed(int requestCode, List grantedPermissions) {
// 权限申请成功回调。
if (requestCode == 101) {
copyFile();
}
}
@Override
public void onFailed(int requestCode, List deniedPermissions) {
// 权限申请失败回调。
// 用户否勾选了不再提示并且拒绝了权限,那么提示用户到设置中授权。
if (AndPermission.hasAlwaysDeniedPermission(MainActivity.this, deniedPermissions)) {
// 第一种:用默认的提示语。
AndPermission.defaultSettingDialog(MainActivity.this, 300).show();
}
}
};
[Android7.0]
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.tianer.ch.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
provider>
<resources>
<paths>
<external-path path="" name="camera.photos" />
paths>
resources>
/**
* 打开apk
*
* @param context
* @param pathName 文件路径
*/
public static void openFile(Context context, String pathName, String authority) {
if (pathName == null) {
return;
}
File file = new File(pathName);
if (file == null || !file.exists()) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(FileProvider.getUriForFile(context, authority, file), "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
context.startActivity(intent);
}
[Android8.0]
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (getPackageManager().canRequestPackageInstalls()) {
FileUtil.openFile(context, dir + "app-debug.apk", "com.tianer.ch.fileprovider");
} else {
// 申请权限。
startInstallPermissionSettingActivity();
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void startInstallPermissionSettingActivity() {
//注意这个是8.0新API
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, 10086);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10086) {
FileUtil.openFile(context, dir + "app-debug.apk", "com.tianer.ch.fileprovider");
}
}