Tsur Yohananov
Tsur Yohananov

Reputation: 543

Sharing an audio file using intent

This is my code:

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+filePath));
startActivity(Intent.createChooser(share, "Share Sound File"));

Problem : For some reason, when I checked today this code on nexus 5X API 26 emulator, the app crashes whereas on my Xperia z5 premium(api 25) it works like a charm.

Is there a chance this code not compatible with the new api?

logcat-

08-24 15:16:08.445 3050-3050/com.example.tsuryohananov.mycallrecorder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.tsuryohananov.mycallrecorder, PID: 3050
                                                                                    android.os.FileUriExposedException: file:////storage/emulated/0/CallRecordings/24.08.2017  02-32-19 PM.m4a exposed beyond app through ClipData.Item.getUri()
                                                                                        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1932)
                                                                                        at android.net.Uri.checkFileUriExposed(Uri.java:2348)
                                                                                        at android.content.ClipData.prepareToLeaveProcess(ClipData.java:941)
                                                                                        at android.content.Intent.prepareToLeaveProcess(Intent.java:9735)
                                                                                        at android.content.Intent.prepareToLeaveProcess(Intent.java:9741)
                                                                                        at android.content.Intent.prepareToLeaveProcess(Intent.java:9720)
                                                                                        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1609)
                                                                                        at android.app.Activity.startActivityForResult(Activity.java:4471)
                                                                                        at android.app.Activity.startActivityForResult(Activity.java:4429)
                                                                                        at android.app.Activity.startActivity(Activity.java:4788)
                                                                                        at android.app.Activity.startActivity(Activity.java:4756)
                                                                                        at com.example.tsuryohananov.mycallrecorder.MainActivity$9.onClick(MainActivity.java:324)
                                                                                        at android.view.View.performClick(View.java:6219)
                                                                                        at android.view.View$PerformClick.run(View.java:24482)
                                                                                        at android.os.Handler.handleCallback(Handler.java:769)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                        at android.os.Looper.loop(Looper.java:164)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6540)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

(line 324 is startActivity)

Upvotes: 0

Views: 1246

Answers (1)

Sahil
Sahil

Reputation: 1419

The problem is already mentioned in the logcat. android.os.FileUriExposedException: file:////storage/emulated/0/CallRecordings/24.08.2017 02-32-19 PM.m4a exposed beyond app through ClipData.Item.getUri()

There are two ways to solve this problem.

  1. Put the following code

    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build());

in Application.onCreate().

we will simply change the VmPolicy by the above method. In this way VM will ignore the exposure URI exposure. Default method is

builder.detectFileUriExposure()

which checks the file for exposure.

  1. Instead of file:// try using content:// In later android version starting from 24 we have to use FileProvider class so that the files are accessible for other apps as well.

I hope it helps, For more on converting from file:// to content://

refer this Question -- https://stackoverflow.com/a/38858040

Upvotes: 2

Related Questions