-1

I tried a code to take screenshot of an Activity then share it , which is works very well but just in Android lower than 6.0 , from 6.0 and higher when i click share button then choose social network a short dialog tell me " unable to load image " Please i need your help my friends This the code that i used it :

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bitmap bitmap = takeScreenshot();
            saveBitmap(bitmap);
            shareIt();
        }

        public Bitmap takeScreenshot() {
            View rootView = findViewById(android.R.id.content).getRootView();
            rootView.setDrawingCacheEnabled(true);
            return rootView.getDrawingCache();
        }
        public void saveBitmap(Bitmap bitmap) {
            imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(imagePath);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                Log.e("GREC", e.getMessage(), e);
            } catch (IOException e) {
                Log.e("GREC", e.getMessage(), e);
            }
        }
        private void shareIt() {
            Uri uri = Uri.fromFile(imagePath);
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("image/*");
            String shareBody = "In Tweecher, My highest score with screen shot";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);

            startActivity(Intent.createChooser(sharingIntent, "Share via"));
        }
    });

    displayResults();
}

EDIT : I try to replace Uri uri = Uri.fromFile(imagePath); by Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider", imagePath); But it says app was stopped when i click Share button Any help pleaaaase

  • try this : https://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot – Logic Jul 31 '18 at 17:55
  • I think Android 6+ added the restriction to accept only content:// URIs and not file:// anymore, you must implement this trough a ContentProvider – Marcos Vasconcelos Jul 31 '18 at 18:28
  • @MarcosVasconcelos thank you, how can i do it , i am beginner in coding – Leo Sanchez Jul 31 '18 at 18:33
  • Possible duplicate of [android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()](https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed) – Marcos Vasconcelos Jul 31 '18 at 19:07

1 Answers1

0

If FileUriExposedException is the exception that is the cause for the issue:-

This exception is thrown when an application exposes a file:// Uri to another app.

This is only thrown for applications targeting Build.VERSION_CODES.N or higher. Applications targeting earlier SDK versions are allowed to share file:// Uri, but it's strongly discouraged.

These links might give a detailed answer to the above problem:-

https://developer.android.com/reference/android/os/FileUriExposedException

android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

Hope this helps.

  • I try to replace Uri uri = Uri.fromFile(imagePath); by Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider", imagePath); But it says app was stopped when i click Share button Any help please – Leo Sanchez Jul 31 '18 at 21:15
  • @LeoSanchez check the link on comments – Marcos Vasconcelos Jul 31 '18 at 21:50
  • @MarcosVasconcelos thank you , i check it and i try it but no solution – Leo Sanchez Jul 31 '18 at 21:58