2016-12-28 5 views
1

Мой код:Android 7.0: Установка .apk

public static void installApk(Context context, File apkFile) { 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     Uri uri = null; 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
      uri = FileProvider.getUriForFile(context, 
     context.getApplicationContext().getPackageName() + ".provider", apkFile); 

      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); 
     } else { 
      uri = Uri.fromFile(apkFile); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     } 
     intent.setDataAndType(uri, "application/vnd.android.package-archive"); 
     context.startActivity(intent); 

    } 

apkFile путь:

/storage/emulated/0/Download/com.me.myapp-1.0.1 .apk

Получить Uri:

content://com.me.myapp.provider/publicDir/Download/com.me.myapp-1.0.1.apk 

Ошибка, которую я не получаю.

ответ

1

Ошибка, созданная моим маленьким игнорированием.

1 манифеста 中 添加 代码

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="${applicationId}.provider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/provider_paths"/> 
    </provider> 

2 Рез/XML/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?> 
    <paths> 
     <external-path 
      name="publicDir" path="/"/> 
    </paths> 

3 установки

public static void installApk(Context context, File apkFile) { 

    Intent intent = new Intent(Intent.ACTION_VIEW); 
    Uri uri = null; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
     uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", apkFile); 
     intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); 
     intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    } else { 
     uri = Uri.fromFile(apkFile); 
    } 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.setDataAndType(uri, "application/vnd.android.package-archive"); 
    context.startActivity(intent); 
} 

вам нужен этот URL

https://developer.android.com/reference/android/os/FileUriExposedException.html

https://developer.android.com/reference/android/support/v4/content/FileProvider.html

код решение моей проблемы. надеюсь, поможет вам

Смежные вопросы