0

I want to install app in my projects. But my code don't work in api 24 or higher. What is its solution?

My code is:

String timestamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
String dir = Environment.getExternalStorageDirectory() + "/" + context.getResources().getString(R.string.cache_path);
String appName = appModel.getAppUrl().substring(appModel.getAppUrl().lastIndexOf('/') + 1, appModel.getAppUrl().length());
appName = timestamp + "_" + appName;

 private void appInstaller(String dir, String appName) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            File file = new File(dir, appName);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            startActivity(intent);
        } else {
            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/" + dir + "/" + appName)), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

Manifest:

 <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

This question is like my question. But does not work its solution for me!

Thanks about to help me!

Kdon Patel
  • 107
  • 1
  • 15
Ehsan Kalali
  • 411
  • 6
  • 16
  • 1
    "it doesn't work" is kinda generic. Does it throw any error? or it simply run through your code without doing anything? You have any "unusual" log in your console? Check [this](https://stackoverflow.com/a/4604922/4700782) answer – Pier Giorgio Misley Nov 20 '19 at 08:39
  • how exactly is it not working? It also should not install apps, but only prompt the user to install them. – Vladyslav Matviienko Nov 20 '19 at 08:47
  • Its log is: `file:///storage/emulated/0/ARMarket/2019.11.21.07.09.51_oloom1.apk exposed beyond app through Intent.getData() E/EGL_emulation: tid 2952: eglSurfaceAttrib(1354): error 0x3009 (EGL_BAD_MATCH)` I don't know that it doesn't work!? – Ehsan Kalali Nov 21 '19 at 12:12

1 Answers1

0

I resolved this problem and this way is true for me: First:

private static final String APP_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyAppFolderInStorage/";

private void install() {
    File file = new File(APP_DIR + fileName);

    if (file.exists()) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String type = "application/vnd.android.package-archive";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri downloadedApk = FileProvider.getUriForFile(getContext(), "ir.greencode", file);
            intent.setDataAndType(downloadedApk, type);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            intent.setDataAndType(Uri.fromFile(file), type);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        getContext().startActivity(intent);
    } else {
        Toast.makeText(getContext(), "ّFile not found!", Toast.LENGTH_SHORT).show();
    }
}

Second: For android 7 and above you should define a provider in manifest like below!

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="ir.greencode"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/paths" />
    </provider>

Third: Define path.xml in res/xml folder like below! I'm using this path for internal storage if you want to change it to something else there is a few way! You can go to this FileProvider

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="your_folder_name" path="MyAppFolderInStorage/"/>
</paths>

Forth: You should add this permission in manifest:

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
Ehsan Kalali
  • 411
  • 6
  • 16