最近学习安卓,想实现一个用代码打开下载的apk的功能。
下载的apk放在了外部储存根目录建的一个文件夹里面,我在真机上测试的,手机是安卓10 api版本是29,但是启动之后提示解析包时出现问题。
尝试了三个apk,两个是unity发布的,还有一个是android studio 发布的,这三个apk单独安装都没问题,排除apk不完整的可能性;unity发布选的api版本是最低的21,按理说也不是api的问题;
另外代码这边也是按照chatgpt写的,代码也很简单,感觉也没问题,我有点懵,希望能有人指导下。
这个是主要逻辑,就这运行这三个方法,我工程下面有个Module包,里面是一些下载的逻辑,只是没有用上。
package com.fch.Android2Unity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_STORAGE_PERMISSION = 1234;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity", "开始执行下载程序!");
//requestStoragePermission2();
/* ProDownLoad pro = new ProDownLoad(MainActivity.this) ;
pro.Install2OpenApk() ;*/
GetAuthority();
}
// 安装权限请求代码
private static final int INSTALL_PERMISSION_REQUEST_CODE = 123;
//安装和启动
private void GetAuthority() {
Log.e("", "开始获取权限");
// 获取安装权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !getPackageManager().canRequestPackageInstalls()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, INSTALL_PERMISSION_REQUEST_CODE);
Log.e("", "没权限");
} else {
// 如果已经有安装权限,则直接安装
Log.e("", "有权限");
installApk();
}
}
// 在Activity中处理权限请求的结果
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == INSTALL_PERMISSION_REQUEST_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && getPackageManager().canRequestPackageInstalls()) {
Log.e("", "开始安装");
// 如果用户授予了安装权限,则进行安装
installApk();
} else {
// 如果用户拒绝了安装权限,则提示用户
Log.e("", "用户拒绝了安装权限");
}
}
}
// 安装APK包
private void installApk() {
File file = new File("/storage/emulated/0/CreateParty/Install/DJ_Install.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(this, "com.fch.Android2Unity.fileprovider", file);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}
}
下面是配置文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE "/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Android2Unity"
tools:targetApi="21">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.fch.Android2Unity.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
provider>
application>
manifest>
file_paths.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path name="create_party" path="."/>
paths>
resources>
build.gradle 文件:
plugins {
id 'com.android.application'
}
android {
namespace 'com.fch.Android2Unity'
compileSdk 33
defaultConfig {
applicationId "com.fch.Android2Unity"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation project(path: ':LoadLib')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
下面是我的工程目录:
我的源码位置
https://gitee.com/fancaihua/android-response.git
基于Monster 组和GPT的调写:
确保文件路径是正确的,可以在代码中添加一些日志来检查路径是否正确,如:
Log.d("MainActivity", "File exists: " + file.exists() + ", path: " + file.getAbsolutePath());
3版本兼容性问题
确保目标 API 和最低 API 版本设置正确,同时也要注意各个版本的行为变化。
例如,在 Android 10 中,对于在外部存储中的文件,应用程序需要在清单文件中声明 MANAGE_EXTERNAL_STORAGE 权限。因此,清单文件应该类似于以下内容:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
...
android:requestLegacyExternalStorage="true"
...
>
...
</application>
</manifest>
4权限问题
确保应用程序具有适当的权限,以便读取和写入外部存储。已经在清单文件中声明了这些权限,但是在 Android 6.0(API 级别 23)及更高版本中,要动态请求这些权限。
试添加以下代码来动态请求存储权限:
private static final int REQUEST_CODE_STORAGE_PERMISSION = 1234;
private void requestStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_STORAGE_PERMISSION);
} else {
// 如果系统版本低于 6.0,则默认具有存储权限
downloadApk();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 如果用户授予了存储权限,则开始下载 APK 文件
downloadApk();
} else {
// 如果用户拒绝了存储权限,则提示用户
Toast.makeText(this, "存储权限被拒绝", Toast.LENGTH_SHORT).show();
}
}
}
然后,在 onCreate() 方法中,可以把 requestStoragePermission2() 替换为 requestStoragePermission()。
以下内容引用CHATGPT:
根据提供的代码和配置文件,可以尝试以下几个方面的解决办法:
在 Android 10 以上,对于外部存储的文件访问需要特定的读写权限,可以尝试在 onCreate()
方法中请求读写权限:
private static final int REQUEST_CODE_STORAGE_PERMISSION = 1234;
private void requestStoragePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_STORAGE_PERMISSION);
} else {
// 已经获得了读写权限,可以进行读写操作
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 获得了读写权限,可以进行读写操作
} else {
// 没有获得读写权限,需要提示用户
}
}
}
可以尝试在 file_paths.xml
文件中更改 path
属性,将其设置为 /
,表示访问整个外部存储:
<paths>
<external-path name="create_party" path="/"/>
</paths>
可以尝试重新下载 APK 文件,并检查下载的 APK 文件是否完整。
可以尝试退出应用程序,或者重启手机,然后再次运行应用程序,看是否能够正常安装 APK 文件。
可以尝试检查 AndroidManifest.xml 文件中的 <application>
标签中是否有重复的属性,或者是否有错误的属性设置。
希望以上解决办法能够帮助到您。
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
代码逻辑中似乎没有问题,但是请尝试更新以下AndroidManifest.xml文件中的权限配置:
1.获取读取外部存储的权限应该只需要其中的<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
,而<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
和<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE "/>
已经被废除,可以删除。
2.由于Android 11及以上版本增加了Scoped Storage机制,需要通过android:requestLegacyExternalStorage="true"
属性以及<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
权限声明让应用支持旧版存储策略。
修改后的AndroidManifest.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Android2Unity"
tools:targetApi="21">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.fch.Android2Unity.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
如果以上修改后还存在问题,请查看Logcat输出,查看是否有其他异常信息,并提供相关信息,以便更好地诊断问题。
如果我的回答解决了您的问题,请采纳!