首页 > 解决方案 > 文件提供者 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,所以很遗憾..

但是当我设置为文本文件时(我在 MainActivity 中更新了这 2 行代码)

String csvFile = "test.txt";
intent.setDataAndType(uri, "text/plain");

菲奥拉,这是可行的。但与 Google play 的文件资源管理器相比,它可以共享给所有应用程序,此代码仅受我本地 android 手机中的 5 个应用程序的限制。使用文件资源管理器吨应用程序可以接收相同的文件。

回到我的问题是如何共享 xls 文件。需要一些明确的解释!

标签: androidxlsandroid-fileprovider

解决方案


推荐阅读