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

View File

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