Merge pull request #5544 from ByteHamster/chooser-intent

Fix Permission Denial when sharing files
This commit is contained in:
ByteHamster 2021-11-14 12:47:33 +01:00 committed by GitHub
commit e41f625488
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 35 deletions

View File

@ -7,7 +7,6 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import com.google.android.material.snackbar.Snackbar;
@ -103,22 +102,21 @@ public class BugReportActivity extends AppCompatActivity {
Runtime.getRuntime().exec(cmd);
//share file
try {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/*");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/*");
String authString = getString(de.danoeh.antennapod.core.R.string.provider_authority);
Uri fileUri = FileProvider.getUriForFile(this, authString, filename);
i.putExtra(Intent.EXTRA_STREAM, fileUri);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
PackageManager pm = getPackageManager();
List<ResolveInfo> resInfos = pm.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfos) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String chooserTitle = getString(de.danoeh.antennapod.core.R.string.share_file_label);
startActivity(Intent.createChooser(i, chooserTitle));
Intent chooser = Intent.createChooser(intent, chooserTitle);
List<ResolveInfo> resInfos = getPackageManager()
.queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfos) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
startActivity(chooser);
} catch (Exception e) {
e.printStackTrace();
int strResId = R.string.log_file_share_exception;

View File

@ -232,15 +232,14 @@ public class ImportExportPreferencesFragment extends PreferenceFragmentCompat {
sendIntent.putExtra(Intent.EXTRA_STREAM, streamUri);
sendIntent.setType("text/plain");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
List<ResolveInfo> resInfoList = getContext().getPackageManager()
.queryIntentActivities(sendIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getContext().grantUriPermission(packageName, streamUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
Intent chooserIntent = Intent.createChooser(sendIntent, getString(R.string.send_label));
List<ResolveInfo> resInfoList = getContext().getPackageManager()
.queryIntentActivities(sendIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getContext().grantUriPermission(packageName, streamUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
getContext().startActivity(Intent.createChooser(sendIntent, getString(R.string.send_label)));
getContext().startActivity(chooserIntent);
});
alert.create().show();
}

View File

@ -5,7 +5,6 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import androidx.core.content.FileProvider;
import android.util.Log;
@ -75,21 +74,20 @@ public class ShareUtils {
}
public static void shareFeedItemFile(Context context, FeedMedia media) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType(media.getMime_type());
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(media.getMime_type());
Uri fileUri = FileProvider.getUriForFile(context, context.getString(R.string.provider_authority),
new File(media.getLocalMediaUrl()));
i.putExtra(Intent.EXTRA_STREAM, fileUri);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
List<ResolveInfo> resInfoList = context.getPackageManager()
.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent chooserIntent = Intent.createChooser(intent, context.getString(R.string.share_file_label));
List<ResolveInfo> resInfoList = context.getPackageManager()
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
context.startActivity(Intent.createChooser(i, context.getString(R.string.share_file_label)));
context.startActivity(chooserIntent);
Log.e(TAG, "shareFeedItemFile called");
}
}