Nikhil Reddy
Nikhil Reddy

Reputation: 83

Sharing app icon in Android 11 not working

Upto Android 9, i am using below code and it is working:

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();
        }

From Android 10 or 11 onwards, this code is not working. I took runtime permissions also, and accepted those. But I see the below errors in logcat: ->Primary directory null not allowed for content://media/external_primary/file; allowed directories are [Download, Documents] ->java.io.FileNotFoundException: /storage/emulated/0/19691213181944618.jpg: open failed: EPERM (Operation not permitted).

Can anyone help me with proper solution? (java)

Upvotes: 0

Views: 858

Answers (0)

Related Questions