intent.setData(Uri.parse(path));
intent.setType("application/android.com.app"); // 錯誤 MIME
intent.setDataAndType(
Uri.fromFile(new File(apkPath)),
"application/vnd.android.package-archive"
);
File apkFile = new File(Environment.getExternalStorageDirectory(), "download/app.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // Android 7+ 必須
startActivity(intent);
file:// Uri,必須用 FileProvider 生成 content:// Uri。<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>Uri uri = FileProvider.getUriForFile(context, authority, file);需引導用戶開啟「允許安裝未知應用」權限,或使用 PackageInstaller 會話安裝(系統應用/設備管理員)。