2016-11-08 73 views
2

嗨stackoverflow即时通讯下载apk安卓Nougat真的很难。我知道文件提供者需要使用,我只是不知道如何输入正确的路径。该应用程序崩溃或找不到该文件。它总是打开/storage/emulated/0/Android/data/cf.vojtechh.apkmirror/Downloads - 哪个当然不存在。我需要的应用程序来存取权限的内部存储(类似Environment.getExternalStorageDirectory()的getPath()+ “/” + Environment.DIRECTORY_DOWNLOADS + “/” +文件名打开下载的文件在Android的牛轧糖与文件提供

任何想法的下载目录?我一直在整个互联网上寻找这一个多小时,但我无法找到它(我到了那一点,我很生气,我想离开那里的崩溃,忽略它)

MainActivity.java

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
         File apkfile = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), fileName); 
         Uri apkUri = FileProvider.getUriForFile(MainActivity.this, "cf.vojtechh.apkmirror.provider", apkfile); 
         String s = apkUri.toString(); 
         Log.d("HERE!!",s); 
         Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); 
         intent.setData(apkUri); 
         intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
         startActivity(intent); 

} else { 
         File apkfile = new File(Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DOWNLOADS + "/" + fileName); 
         Uri apkUri = Uri.fromFile(apkfile); 
         Intent intent = new Intent(Intent.ACTION_VIEW); 
         intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); 
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         startActivity(intent); 
} 

provider_paths.xml

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

AndroidManifest.xml中

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="cf.vojtechh.apkmirror.provider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/provider_paths"/> 
    </provider> 

回答

2

尝试使用Environment#getExternalStoragePublicDirectory()方法代替。 Context#getExternalFilesDir()旨在用于私人应用程序的文件,因此包含应用程序包名称的路径(技术上,此目录公开,但仍不是您要查找的内容)。

File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 

然后你就可以创建下载的文件的File表示:

File apk = new File(downloads, filename);