Я работаю над установкой 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>
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