Victor Hugo Montes
Victor Hugo Montes

Reputation: 1322

Key android.intent.extra.STREAM expected Parcelable but value was a java.io.File. The default value <null> was returned

I am trying to share an .mp3 file from the cache folder by doing this:

InputStream inputStream = getContentResolver().openInputStream(UriUtils.getResourceUri(audioToShare.getAudioPath(),this));
                File fileToShare = FileUtils.writeStreamToFile(this,audioToShare.getLabel(),inputStream);
                Intent intent = new Intent(Intent.ACTION_SEND).setType("audio/mpeg");
                intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(fileToShare));
                startActivity(Intent.createChooser(intent, "Share to"));

But then when I pick a contact, let's say on whatsapp, the app throws bellow's warning.

W/Bundle: Key android.intent.extra.STREAM expected Parcelable but value was a java.io.File.  The default value <null> was returned.
12-25 16:10:32.802 17021-17021/com.example.soundboard W/Bundle: Attempt to cast generated internal exception:
                                                                       java.lang.ClassCastException: java.io.File cannot be cast to android.os.Parcelable
                                                                           at android.os.Bundle.getParcelable(Bundle.java:894)
                                                                           at android.content.Intent.getParcelableExtra(Intent.java:7075)
                                                                           at android.content.Intent.migrateExtraStreamToClipData(Intent.java:9887)
                                                                           at android.content.Intent.migrateExtraStreamToClipData(Intent.java:9867)
                                                                           at android.app.Instrumentation.execStartActivity(Instrumentation.java:1608)
                                                                           at android.app.Activity.startActivityForResult(Activity.java:4472)
                                                                           at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                                                                           at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
                                                                           at android.app.Activity.startActivityForResult(Activity.java:4430)
                                                                           at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
                                                                           at android.app.Activity.startActivity(Activity.java:4791)
                                                                           at android.app.Activity.startActivity(Activity.java:4759)
                                                                           at com.quartzodev.cirosoundboard.sound.SoundActivity.onActionItemClicked(SoundActivity.java:170)

Method writeStreamToFile so that you can see how I create the cached file

public static File writeStreamToFile(Context context, String fileName, InputStream input){

        try {
            File file = new File(context.getFilesDir(), fileName);
            OutputStream output = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[4 * 1024]; // or other buffer size
                int read;

                while ((read = input.read(buffer)) != -1) {
                    output.write(buffer, 0, read);
                }

                output.flush();

                return file;
            } finally {
                output.close();

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

What am I doing wrong?

Upvotes: 3

Views: 4795

Answers (1)

Victor Hugo Montes
Victor Hugo Montes

Reputation: 1322

Turns out that I had no permission to share files from folders in getFilesDir()

Just changed to getExternalCacheDir() and now I can share through Whatsapp and Gmail

Upvotes: 1

Related Questions