0

No Error, but nothing to share, just open another app.

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"));

I was try a lot of trial, but still stuck.

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>

Grade build

    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'
            }
        }
    }

Just read alot of reference and still stuck, my background isn't android so sory if some bad code..

But when i set to text file(i update this 2 line code in MainActivity)

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

And fiola, this is works. But compared with File Explorer from Google play it could share to all app, this code just limited by 5 apps in my local android phone. With File Explorer tons apps could received the same file.

Back to my question is how to share xls file. Need some clear explanation!

budi
  • 53
  • 6
  • 1
    set intent.setDataAndType(uri, "text/csv"); have you tried it – rachna Oct 23 '19 at 09:58
  • It didnt appear on official MIME type i ever read in other link, but it's **works**,, But it just open file, not share the file, maybe my code wrong in somewhere ? – budi Oct 23 '19 at 10:00
  • where you want to share this folder – rachna Oct 23 '19 at 10:05
  • you should share your file from external storage – rachna Oct 23 '19 at 10:06
  • Woaaah,,, just update from `Intent intent = new Intent(Intent.ACTION_VIEW);` to `Intent intent = new Intent();` and done. post it in answer, i'll note it as answer @rachna – budi Oct 24 '19 at 01:27

0 Answers0