Нет ошибок, но поделиться нечего, просто откройте другое приложение.
MainActivity
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'
}
}
}
Просто прочитайте много ссылок и все еще застряли, мой фон не является андроидным, так что если какой-то плохой код ..
Но когда я устанавливаю текстовый файл (я обновляю этот 2-строчный код в MainActivity)
String csvFile = "test.txt";
intent.setDataAndType(uri, "text/plain");
И, фиола, это работает. Но по сравнению с File Explorer из Google Play, он может использоваться всеми приложениями, этот код ограничен 5 приложениями в моем локальном телефоне Android. С помощью File Explorer тонны приложений могли получать один и тот же файл.
Вернемся к моему вопросу, как поделиться файлом xls. Нужно четкое объяснение!