android - 文件提供者 Android 共享 xls
问题描述
没有错误,但没有什么可分享的,只需打开另一个应用程序。
主要活动
String csvFile = "Mytest23Okt.xls";
// String csvFile = "empty";
sd = this.getCacheDir();
directory = new File(sd.getAbsolutePath());
// Intent sharingIntent = new Intent(Intent.ACTION_SEND);
// Uri UriData = Uri.parse("file://" + directory + "/" + csvFile);
// sharingIntent.setType("text/plain");
// sharingIntent.setType("*/*");
// sharingIntent.setType("application/x-excel");
// sharingIntent.setType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
// sharingIntent.setType("application/vnd.ms-excel");
// sharingIntent.setType("application/excel");
// sharingIntent.setType("application/x-mexcel");
// sharingIntent.putExtra(Intent.EXTRA_STREAM, UriData);
// sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// startActivity(Intent.createChooser(sharingIntent, "Share data using"));
// create new Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
// set flag to give temporary permission to external app to use your FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//file from dir
// File myObj = new File("content://"+directory+"/"+csvFile);
// Specify the filename
//Log.d(TAG, "shareFunction:"+myObj.getAbsolutePath());
File imagePath = new File(this.getCacheDir(), "files");
File newFile = new File(imagePath, csvFile);
// generate URI, I defined authority as the application ID in the Manifest, the last param is file I want to open
// String uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, myObj);
Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID, newFile);
Log.d(TAG, "shareFunction: "+uri);
intent.setDataAndType(uri, "*/*");
// intent.setDataAndType(uri, "application/vnd.ms-excel");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "Share data using"));
我尝试了很多试验,但仍然卡住了。
Provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<cache-path name="cache" path="/" />
<files-path name="files" path="/" />
</paths>
androidManifest.xml
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
等级构建
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.m.test2excel"
minSdkVersion 14
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
只是阅读了很多参考资料,但仍然卡住了,如果代码不好,我的背景不是 android,所以很遗憾..
- 使用文件提供程序检索内容 Uri 失败
- 在 Android 应用程序中添加多个文件提供程序
- 如何使用支持 FileProvider 将内容共享给其他应用程序?
- https://developer.android.com/reference/android/support/v4/content/FileProvider
但是当我设置为文本文件时(我在 MainActivity 中更新了这 2 行代码)
String csvFile = "test.txt";
intent.setDataAndType(uri, "text/plain");
菲奥拉,这是可行的。但与 Google play 的文件资源管理器相比,它可以共享给所有应用程序,此代码仅受我本地 android 手机中的 5 个应用程序的限制。使用文件资源管理器吨应用程序可以接收相同的文件。
回到我的问题是如何共享 xls 文件。需要一些明确的解释!
解决方案
推荐阅读
- amazon-web-services - 使用 AWS cli 设置 Auto Scaling 组执行策略
- c - 冒泡排序扑克牌
- android - TextUtils.join 在单元测试中返回 NULL
- django - Django 翻译使消息丢失#。在 .po 文件中
- visual-studio-code - 无法在 macOS 的终端中打开通过 anaconda 安装的 vscode
- manifoldjs - PWABuilder 网页浏览器
- android - 在 xamarin android native 中滑动 tablayout 后从导航视图中选择菜单项时出现相同的视图
- python - 如何使用 Python 从文本文件中提取某些关键字
- java - Bukkit(spigot api)监听器没有响应?
- hibernate - Hibernate - PooledOptimizer 生成方法中的逻辑