如何解决在 Android 11 中共享应用程序图标不起作用
到 Android 9,我使用下面的代码并且它正在工作:
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
OutputStream fOut;
Uri outputFileUri;
try {
int time = (int) (System.currentTimeMillis());
Timestamp tsTemp = new Timestamp(time);
String timestamp = tsTemp.toString();
File root = new File(Environment.getExternalStorageDirectory() + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root,timestamp + ".jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
bm.compress(Bitmap.CompressFormat.PNG,100,fOut);
fOut.flush();
fOut.close();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("*/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM,"android.resource://" + getPackageName() + "/drawable/ic_launcher");
sharingIntent.putExtra(Intent.EXTRA_STREAM,outputFileUri);
String shareBody = "Download our app now";
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"Easy Mobile");
sharingIntent.putExtra(Intent.EXTRA_TEXT,shareBody);
startActivity(Intent.createChooser(sharingIntent,"Share"));
} catch (Exception e) {
e.printStackTrace();
}
从 Android 10 或 11 开始,此代码不起作用。我也获得了运行时权限,并接受了这些权限。但我在 logcat 中看到以下错误: -> content://media/external_primary/file 不允许主目录为空;允许的目录是 [下载、文档] ->java.io.FileNotFoundException:/storage/emulated/0/19691213181944618.jpg:打开失败:EPERM(不允许操作)。
谁能帮我找到合适的解决方案? (java)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。