gpt4 book ai didi

Android 共享应用程序的 apk 使用 FileProvider

转载 作者:行者123 更新时间:2023-12-05 00:14:11 27 4
gpt4 key购买 nike

我尝试通过 intent 共享应用程序的 apk 文件。我的 list 文件中的提供者:

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

mypaths 文件是:

<paths>
<external-path name="apk_folder"/>
</paths>

我将 Intent 和文件路径设置如下:

String packageName = getContext().getPackageName();
PackageManager pm = getContext().getPackageManager();
String apk = null;
try {
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
apk = ai.publicSourceDir;

} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
File apkFile = new File(apk);

Uri uri = FileProvider.getUriForFile(getContext(), "package name", apkFile);

Intent intent = ShareCompat.IntentBuilder.from(getActivity())
.setType("*/*")
.setStream(uri)
.setChooserTitle("Share via")
.createChooserIntent()
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(intent);

我得到 IllegalArgumentException :

Failed to find configured root that contains /data/app/package name.edu-1/base.apk

请帮我找出我的错误。

最佳答案

将代码中的“包名”更改为activity.getPackageName()并将路径更改为root-path

或者试试这个代码:

list

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

res/xml/file_paths.xml

<paths>
<root-path
name="app"
path="/"/>
</paths>

代码:

public static void sendApkFile(Activity activity) {
try {

PackageManager pm = activity.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(activity.getPackageName(), 0);
File srcFile = new File(ai.publicSourceDir);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
Uri uri = FileProvider.getUriForFile(context, activity.getPackageName(), srcFile);
intent.putExtra(Intent.EXTRA_STREAM, uri);
activity.grantUriPermission(activity.getPackageManager().toString(), uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);

} catch (Exception e) {
e.printStackTrace();
}
}

如果你想让 APK 文件有任何其他名称,而不仅仅是 base.APK,试试这个

public static void sendApplication(Activity activity) {
ApplicationInfo app = activity.getApplicationContext().getApplicationInfo();
String filePath = app.sourceDir;

Intent intent = new Intent(Intent.ACTION_SEND);

// MIME of .apk is "application/vnd.android.package-archive".
// but Bluetooth does not accept this. Let's use "*/*" instead.
intent.setType("*/*");

// Append file and send Intent
File originalApk = new File(filePath);

try {
//Make new directory in new location
File tempFile = new File(activity.getExternalCacheDir() + "/ExtractedApk");
//If directory doesn't exists create new
if (!tempFile.isDirectory()) {
if (!tempFile.mkdirs()) {
return;
}
}
//Get application's name and convert to lowercase
tempFile = new File(tempFile.getPath() + "/" + activity.getString(app.labelRes).replace(" ", "").toLowerCase() + ".apk");
//If file doesn't exists create new
if (!tempFile.exists()) {
if (!tempFile.createNewFile()) {
return;
}
}
//Copy file to new location
InputStream in = new FileInputStream(originalApk);
OutputStream out = new FileOutputStream(tempFile);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
//Open share dialog

Uri uri = FileProvider.getUriForFile(context, activity.getPackageName(), tempFile);
intent.putExtra(Intent.EXTRA_STREAM, uri);
activity.grantUriPermission(activity.getPackageManager().toString(), uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);

} catch (Exception e) {
e.printStackTrace();
}
}

关于Android 共享应用程序的 apk 使用 FileProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44522278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com