2

I working on installing APK file present in the device programmatically. Here is my code.

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 - File provider to enable other application to install. /****************************************************************************/

<?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 - File provider to point external storage. /****************************************************************************/

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
  • here: https://stackoverflow.com/questions/1492401/parse-error-there-is-a-problem-parsing-the-package-while-installing-android – Rohit Sharma May 28 '18 at 12:28
  • 2
    Possible duplicate of ["Parse Error : There is a problem parsing the package" while installing Android application](https://stackoverflow.com/questions/1492401/parse-error-there-is-a-problem-parsing-the-package-while-installing-android) – Rohit Sharma May 28 '18 at 12:29

1 Answers1

1
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.

To solve this error

  1.Go to the settings
  2.Scroll down & select option “security.”
  3.Check on the option “Unknown sources.”

To enable USB debugging:

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."
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13