Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: share CSV file

Tags:

android

csv

share

My aplication can generate CSV files that I want to share. I'm using MIME type text/comma_separated_values/csv, but when I send the Intent the chooser isn't shown, I guess my device doesn't know how to handle the file. Which type should I use?

This is my code:

    Uri csv = lh.createDailyCSV();

    if(csv == null){
         Toast.makeText(this, getString(R.string.error_creating_csv), Toast.LENGTH_LONG).show();
    }
    else{
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/comma_separated_values/csv");
        sharingIntent.setData(csv); 
        startActivity(Intent.createChooser(sharingIntent, getResources().getText(R.string.send_to)));
    }   

I declared in my manifest:

    <intent-filter>
        <action android:name="android.intent.action.SEND" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="text/comma_separated_values/csv" />
    </intent-filter>

And I get the exception

03-12 12:19:23.430: E/ActivityThread(24011): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@412fc920 that was originally registered here. Are you missing a call to unregisterReceiver()?

I've read this Exception occurs when there is none or just 1 option in the chooser.

[EDIT]

I changed how I attach the data to the Intent. Instead of sharingIntent.setData(csv) I used:

sharingIntent.putExtra(Intent.EXTRA_STREAM, csv);

And now the chooser works fine, but if I try to send the file via e-mail I get an error: File couldn't be shown.

[/EDIT]

like image 557
PX Developer Avatar asked Mar 12 '13 11:03

PX Developer


1 Answers

The correct MIME type would be text/csv. If that doesn't work, you can use text/plain which will allow the user to potentially choose from a lot of apps, including Evernote etc.

Update After the update, it seems that you don't want to "share" the file with arbitrary other apps but only send it by e-mail? Please clarify.

like image 52
class stacker Avatar answered Sep 28 '22 06:09

class stacker