android - 在android Nougat中安装apk文件时如何解决解析包错误
问题描述
我正在以编程方式安装设备中存在的 APK 文件。这是我的代码。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
askForPermission(Manifest.permission.READ_EXTERNAL_STORAGE,0x4);
askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,0x3);
Button button = findViewById(R.id.install);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", new File(Environment.getExternalStorageDirectory()+"/ExtractedApks/Airtel_TV_tv.accedo.airtel.wynk.apk"));
//Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", new File(Environment.getExternalStorageDirectory()+"/Download/Airtel_TV_tv.accedo.airtel.wynk.apk"));
intent.setDataAndType(photoURI, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
});
}
private void askForPermission(String permission, Integer requestCode) {
if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
}
} else {
return;
//Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
}
}
}
**MainActivity** - create a button to install apk file store in external memory.
/************************************************* *************************/
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.installapkfiles">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false"
android:authorities="${applicationId}.provider">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"/>
</provider>
</application>
</manifest>
清单- 使其他应用程序能够安装的文件提供程序。/************************************************* ******************************/
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- <cache-path name="cache" path="." />
<files-path name="files" path="."/>
<external-path name="external_files" path="."/>-->
<external-path path="Android/data/${applicationId}/" name="files_root" />
<root-path name="root" path="/" />
<!--<external-path path="." name="external_storage_root" />-->
</paths>
File_provider - 指向外部存储的文件提供程序。/************************************************* ******************************/
I'm selecting package installer for installing the application. But, it is poping "There was a problem while parsing the package".
How to resolve this error.
Thanks & Regards,
Gowtham M
解决方案
1.File may be downloaded incompletely.
2.Application might be not suitable for your hardware or OS version.
3.Due to security issue settings
4.Corrupted APK file.
为了解决这个错误
1.Go to the settings
2.Scroll down & select option “security.”
3.Check on the option “Unknown sources.”
要启用 USB 调试:
1. Go to the settings >> Scroll down then, at last, you will see
option “About device” select it.
2. Look for option “build number.” Tap on “Build number” for 7 times.
3. You will see a message “you are now a developer.” Once you enable to
4. go back to settings Choose “Developer options.” Tick mark "USB
debugging."
推荐阅读
- javascript - React Native Flatlist 搜索没有从 firebase 用户集合中返回任何值
- python - 使用 BeautifulSoup 抓取特定国家/地区的产品页面
- django - 删除带有数据库中模型的第三方 Django 应用程序的最佳方法是什么?
- google-oauth - 在 S3 存储桶上使用 json 文件的 Google API 的 OAuth2 问题
- wordpress - 缺少 Wordpress 多站点博客用户角色
- ios - 如何在 SwiftUI 中使用 NumberFormatter?
- asp.net-core - 如何将 .net5 应用部署到 Google Cloud Appengine?
- semantics - 在编程语言语义中如何构造一个语句来显示 S1;S2 和 S2;S3 在语义上不相等?
- mysql - 将 3 个 mysql 查询组合成一个查询
- azure - 组合复杂的 Kusto 查询