I have a difficulty in using ShareCompat and FileProvider to share Audio file to WhatsApp, Telegram, etc

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



I have a difficulty in using ShareCompat and FileProvider to share Audio file to WhatsApp, Telegram, etc



I have a difficulty in Android Studio using ShareCompat and FileProvider to share Audio file... I use 1 Audio Files As an Example :


ShareCompat


FileProvider



/storage/emulated/0/Music/JPlayer/Cornerstone.m4a



If I run my code and share Audio File to Whatsapp, It will show in Whatsapp this message : "Sharing Failed, please try again"
If I run my code and share Audio File to Telegram, It will show in Telegram this message : Unsupported Attachment



This is my Code :



MainActivity.java


public class MainActivity extends AppCompatActivity
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;


@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, Array(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

final File file = new File("/storage/emulated/0/Music/JPlayer/Cornerstone.m4a");
final Uri uri = FileProvider.getUriForFile(this, this.getPackageName() + ".provider", file);
Intent intent = ShareCompat.IntentBuilder.from(this)
.setType("audio/*")
.setStream(uri)
.setChooserTitle("Choose File client")
.getIntent();

List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) Intent.FLAG_GRANT_READ_URI_PERMISSION);


startActivity(intent);


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults)
for (int i : grantResults)
if (i == PackageManager.PERMISSION_DENIED)
return;


switch (requestCode)
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:

break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);






Manifest.xml


<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="$applicationId.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>



xml/provider_paths.xml


<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS